使用jQuery.ajax提交文件会产生TypeError

我试图使用jQuery的ajax方法从表单提交文件:

 var ofile=document.getElementById('image').files[0]; var formdata = new FormData(); formdata.append("image",ofile); $.ajax({ url:'elements/save_elements', data:formdata, type:'POST' }); 

这会导致错误TypeError: 'append' called on an object that does not implement interface FormData

是什么导致这个错误? 它不会发生在实际的formdata.append ,而是发生在jQuery中。

我遇到了类似代码的同样问题。 关于此错误的信息严重缺乏,因此OP没有详细说明:

通过一些调试,我意识到错误是由jquery深度的ajax调用引发的,而不是实际的追加。 结果我忘了在ajax请求中添加processData: false, contentType: false ; 这样做可以解决问题。

将以下内容添加到ajax对象时,它可以正常工作:

 contentType: false, processData: false, 

所以看起来应该是这样的:

 $.ajax({ url:'elements/save_elements', data:formdata, type:'POST', contentType: false, processData: false, }); 

将这些参数添加到ajax可以解决问题

 $.ajax({ url: 'upload_ajax.php', type: 'POST', data: formData, contentType: false, processData: false,