Javascript:TypeError:Value没有实现接口FormData [英] Javascript: TypeError: Value does not implement interface FormData

查看:78
本文介绍了Javascript:TypeError:Value没有实现接口FormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FormData通过AJAX将数据发送到PHP脚本。
输入类型文本值似乎没有任何问题,但是当我尝试追加文件时,我得到错误TypeError:Value没有实现接口FormData。

I'm trying to use FormData to send data via AJAX to a PHP script. There doesn't seem to be any problem with the input type text values but when i try to append files i get the error TypeError: Value does not implement interface FormData.

我是FormData的新手,但我在网上搜索过,找不到关于此错误的任何文档。

I am new to FormData, but i searched on the web and couldn't find any doc on this error.

以下是表格:

<form id="item_form" class="item_form" enctype="multipart/form-data">
    <div class="">
        <label for="emp_photos">photos</label>
        <input id="emp_photos" class="inputText" type="file" value="" name="emp_photos">
    </div>
</form>

这里是Javascript:

here'S the Javascript :

var formData = new FormData();      
formData.append('photos', $('#emp_photos').files[0]);

这是我在firebug中得到的错误:

here's the error i get in firebug :

TypeError: Value does not implement interface FormData. 

...igger("ajaxComplete",[N,p]),--b.active||b.event.trigger("ajaxStop")))}return N},...

jquery....min.js (line 5)

我在这里做错了什么?

编辑:ajax部分

$.ajax({
   type: 'POST',
   url: '"; 
   echo $_SESSION["url_base"];
   echo "operations/add_employes',
   data: formData,
   xhr: function() {  // custom xhr
      myXhr = $.ajaxSettings.xhr();
      if(myXhr.upload) { // check if upload property exists
         myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
      }
      return myXhr;
   },
   success: function(msg) {/*...*/}

});


推荐答案

var inputs = $("input[type=file]"),
    files = [];

// jquery or javascript have a slightly different notation
// it's either accessing functions () or arrays [] depending on which object you're holding at the moment
for (var i = 0; i < inputs.length; i++){
    files.push(inputs.eq(i).prop("files")[0]);
    //files.push(inputs[i].files[0]);
    //filename = inputs[i].files[0].name;
    //filesize = inputs[i].files[0].size;
}

if (formdata) {
    // you can use the array notation of your input's `name` attribute here
    formdata.append("emp_photos[]", files);
}

这篇关于Javascript:TypeError:Value没有实现接口FormData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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