使用 jQuery.ajax 提交文件会导致 TypeError [英] Submitting a file with jQuery.ajax gives TypeError

查看:30
本文介绍了使用 jQuery.ajax 提交文件会导致 TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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' 在未实现接口 FormData 的对象上调用.

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

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

推荐答案

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

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:

通过一些调试,我意识到错误是由 jquery 深处的 ajax 调用引发的,而不是实际的追加.原来我忘记将 processData: false, contentType: false 添加到 ajax 请求中;这样做解决了问题.

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 提交文件会导致 TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆