I am trying to submit a file from a form using jQuery's ajax method:

我試圖使用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'

});

This results in the error TypeError: 'append' called on an object that does not implement interface FormData.

這會導致錯誤TypeError:'append'在未實現接口FormData的對象上調用。

What causes this error? It doesn't happen on the actual formdata.append, but inside jQuery.

是什么導致這個錯誤?它不會發生在實際的formdata.append上,而是發生在jQuery中。

3 个解决方案

#1

98

I was having the same problem with similar code. There's a severe dearth of information about this error, so since the OP didn't elaborate:

我遇到了類似代碼的同樣問題。關於此錯誤的信息嚴重缺乏,因此OP沒有詳細說明:

With some debugging I realised the error was being thrown by the ajax call in the depths of jquery, not the actual append. Turns out I'd forgotten to add processData: false, contentType: false to the ajax request; Doing so fixed the problem.

通過一些調試,我意識到錯誤是由jquery深度的ajax調用引發的,而不是實際的追加。結果我忘了在ajax請求中添加processData:false,contentType:false;這樣做可以解決問題。

#2

1

It works fine when you add the the following to the ajax object:

將以下內容添加到ajax對象時,它可以正常工作:

contentType: false,

processData: false,

So it should look like:

所以看起來應該是這樣的:

$.ajax({

url:'elements/save_elements',

data:formdata,

type:'POST',

contentType: false,

processData: false,

});

#3

0

Adding these parameter to ajax solve the problem

將這些參數添加到ajax可以解決問題

$.ajax({

url: 'upload_ajax.php',

type: 'POST',

data: formData,

contentType: false,

processData: false,

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐