上传图片与其他信息与ajax, jquery和PHP


Uploading Image with other information with ajax, jquery and PHP

我有使用ajax + php的图像上传信息的问题。在尝试添加dataResult时,我得到了一条非法调用消息。File = form_data;如下面的代码所示:

我将非常感谢任何帮助!我被困在这个问题上,已经搜索了stackoverflow相当多的答案,可以帮助我。

function uploadImage (upFile,userId,description,category) {
    console.log("We have reached the point where we connect to the service to upload the image...");
    var dataResult = {};
    var form_data = new FormData();
    form_data.append('file',upFile[0]);
    console.log("Is the information on the form data variable?");
    console.log(form_data);
    dataResult.file = form_data;
    dataResult.idUser = userId;
    dataResult.description = description; 
    dataResult.nameCat = category;
    console.log("Description " + dataResult.description);
    console.log("Id User " + dataResult.idUser);
    console.log("name Cat " + dataResult.nameCat);
    console.log("This is the information that is being sent thorugh ajax");
    console.log(dataResult);
    $.ajax({
            url: 'http://localhost/App/www/classes/actionsPhotos/uploadPhoto.php',
            type: 'post',
            data: dataResult,
            success: function (data) {
                console.log("All is good and working.");
                console.log(data);
            },
            error: function(errorInfo){
                console.log("There was an error: ");
                console.log(errorInfo);
            }
        });
}

好的,我通过稍微切换代码来解决这个问题,这里是新代码,供有同样问题的人使用。

函数uploadImage (upFile,userId,description,category) {console.log("我们已经连接到上传图像的服务…");var form_data = new FormData();form_data.append("文件",upFile [0]);form_data.append (idus, userId);form_data.append("描述",描述);form_data.append (nameCat,类别);

$.ajax({
        url: 'http://localhost/App/www/classes/actionsPhotos/uploadPhoto.php',
        type: 'post',
        data: form_data,
        cache: false,
        processData: false,
        contentType: false,
        success: function (data) {
            console.log("All is good and working.");
            console.log(data);
        },
        error: function(errorInfo){
            console.log("There was an error: ");
            console.log(errorInfo);
        }
    });

}