【问题标题】:formData object not working with jquery AJAX post?formData 对象不适用于 jquery AJAX 帖子?
【发布时间】:2015-12-01 23:56:36
【问题描述】:

让我们直接进入代码:

var formData = new FormData();

formData.append('name', dogName);
formData.append('weight', dogWeight);
formData.append('activity', dogActivity);
formData.append('age', dogAge);
formData.append('file', document.getElementById("dogImg").files[0]);
console.log(formData);

这里我将一些字符串和一个文件对象附加到 formData 对象,以便将所有信息异步发送到服务器。

之后我有这个 jquery ajax 请求:

$.ajax({
  type: "POST",
  url: "/foodoo/index.php?method=insertNewDog",
  data: JSON.stringify(formData),
  processData: false,  // tell jQuery not to process the data
  contentType: "multipart/form-data; charset=utf-8",
  success: function(response){
     console.log(response);
  },
  error: function(){
  }
});

所以我在这里尝试将信息 POST 到服务器,在服务器 php 文件上我有一个简单的 POST print_r,所以我可以看到什么通过,什么不通过。

很遗憾,我在 console.log(data) 中的回复是空的。

此外,如果您检查“网络”选项卡中的 HEADER,您会得到以下空输出:

成功函数被调用(只是为了澄清)

【问题讨论】:

  • JSON.stringify 仅适用于 plain 对象/数组。 FormData 不是普通对象。你必须通过FormData.getAll() 而不是FormData
  • 数据:JSON.stringify(formData.getAll()) ?这会导致 Uncaught TypeError: formData.getAll is not a function
  • 那么就不要使用JSON.stringify了。按原样传递formData
  • 好的,当我按原样发送它时 - 然后 print_r 将 php 文件中的对象和 console.log 它作为响应我得到一个简单的 ---- 但是请求有效负载在Network 中的 Header TAB 现在填写了发送数据

标签: javascript jquery ajax form-data


【解决方案1】:
//For those who use plain javascript

var form = document.getElementById('registration-form'); //id of form
var formdata = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open('POST','form.php',true);
// xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
//if you have included the setRequestHeader remove that line as you need the 
// multipart/form-data as content type.
xhr.onload = function(){
 console.log(xhr.responseText);
}
xhr.send(formdata);

【讨论】:

  • 回答时,请考虑解释原始问题中的错误/问题以及您是如何解决的。不要忘记 StackOverflow 上的每个问题都被许多用户和非用户查看,以解决他们类似的问题。因此,为了让他们从您的回答中受益,每个人都希望通过您的解释。
  • 这个答案拯救了我的一天。 !!!
【解决方案2】:

如果您完全按照之前的回答进行了操作,但仍然无法正常工作 别担心its working

也许intelligence and quick wath 告诉你它的not working

不过别担心,看network tap

它的工作原理

希望这可以节省您的时间

【讨论】:

    【解决方案3】:

    当您通过 jQuery 发送 ajax 请求并且想要发送 FormData 时,您不需要在此 FormData 上使用 JSON.stringify。此外,当您发送文件时,内容类型必须是 multipart/form-data,包括 boundry - 类似于 multipart/form-data; boundary=----WebKitFormBoundary0BPm0koKA

    因此,要通过 jQuery ajax 发送包含一些文件的 FormData,您需要:

    • data 设置为FormData,无需任何修改。
    • processData 设置为false(可以防止jQuery 自动将数据转换为查询字符串)。
    • contentType 设置为false(这是必需的,否则jQuery 会错误地设置它)。

    您的请求应如下所示:

    var formData = new FormData();
    
    formData.append('name', dogName);
    // ... 
    formData.append('file', document.getElementById("dogImg").files[0]);
    
    
    $.ajax({
        type: "POST",
        url: "/foodoo/index.php?method=insertNewDog",
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            console.log(response);
        },
        error: function(errResponse) {
            console.log(errResponse);
        }
    });
    

    【讨论】:

    • 我已将我的代码增强为您的代码 - 不幸的是,响应是空的 :( 空我并不是指“null”左右,它只是一个空的 console.log
    • 这对我不起作用,在这里查看我的问题stackoverflow.com/questions/35954488/…
    • formData.append('name', $("#idofinputfiled").val());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多