【问题标题】:Uploading Image with other information with ajax, jquery and PHP使用 ajax、jquery 和 PHP 上传带有其他信息的图像
【发布时间】:2015-12-18 15:22:56
【问题描述】:

我在使用 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);
            }
        });
}

【问题讨论】:

    标签: javascript php jquery ajax file-upload


    【解决方案1】:

    好的,我通过稍微切换代码解决了这个问题,这是适用于遇到同样问题的任何人的新代码。

    函数uploadImage(upFile,userId,description,category) { console.log("我们已经到了连接服务上传图片的地步..."); var form_data = new FormData(); form_data.append('file',upFile[0]); form_data.append('idUser',userId); form_data.append('description',description); form_data.append('nameCat',category);

    $.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);
            }
        });
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-29
      • 2017-05-25
      • 1970-01-01
      • 2020-07-09
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多