提交文件,jQuery.ajax给类型错误 [英] Submitting a file with jQuery.ajax gives TypeError

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

问题描述

我试图使用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'
});

这导致错误类型错误:所谓的对象不实现接口FORMDATA在追加

是什么原因导致这个错误?它没有实际发生 formdata.append ,但里面的jQuery。

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

推荐答案

我是有类似code同样的问题。有关于这个错误信息的严重匮乏,如此以来,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:

通过一些调试,我意识到正在由Ajax调用jQuery中,而不是实际的追加深处抛出的错误。
原来我忘了添加过程数据:假的contentType:假 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给类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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