TypeError: 'append' called on an object that does not implement interface FormData.

平时做表单都是跳转提交的,但是今天要做一个ajax图片异步上传,

网上搜索了下,方法都比较老了,居然还有用flash的,

普通的表单上传通过jquery的serialize()转换成querystring后就可以直接ajax post 上传,但是碰到文件上传就不奏效了,型号html5有个方法FormData()可以实现上传,

我写的代码如下:

function upThumbSubmit() {
if(!window.FormData) { 
        alert('your brower is too old');
        return false;
    }
 var formData = new FormData($( "#upForm" )[0]);

  $.ajax({
        url:'?c=api&a=upload',
        type:'post',
        data:formData,
        dataType:'json',
        success:function(data){
          alert(data);
          return false;

        }
  });


}

但是报错了,错误如下

TypeError: 'append' called on an object that does not implement interface FormData.

既然浏览器明明显示支持formdata,为何这里显示append不是formdata接口呢?

答案只可能是jquery重载了formdata

在里面加上2个option,就好了,正确代码如下

function upThumbSubmit() {
if(!window.FormData) { 
        alert('your brower is too old');
        return false;
    }
 var formData = new FormData($( "#upForm" )\[0\]);

  $.ajax({
        url:'?c=api&a=upload',
        type:'post',
        data:formData,
        processData: false,
        contentType: false,
        dataType:'json',
        success:function(data){
          alert(data);
          return false;

        }
  });


}