jQuery Javascript: TypeError: Value 没有实现接口 FormData
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16346714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: TypeError: Value does not implement interface FormData
提问by Pascal montpetit
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 通过 AJAX 将数据发送到 PHP 脚本。输入类型文本值似乎没有任何问题,但是当我尝试附加文件时,出现错误 TypeError: Value does not implement interface FormData。
I am new to FormData, but i searched on the web and couldn't find any doc on this error.
我是 FormData 的新手,但我在网上搜索并找不到有关此错误的任何文档。
Here's the form :
这是表格:
<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>
here'S the Javascript :
这是 Javascript :
var formData = new FormData();
formData.append('photos', $('#emp_photos').files[0]);
here's the error i get in firebug :
这是我在 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)
What am i doing wrong here ?
我在这里做错了什么?
EDIT: ajax part
编辑: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) {/*...*/}
});
采纳答案by Tim Vermaelen
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);
}