gpt4 book ai didi

Javascript:TypeError:值未实现接口(interface) FormData

转载 作者:搜寻专家 更新时间:2023-11-01 04:32:36 26 4
gpt4 key购买 nike

我正在尝试使用 FormData 通过 AJAX 将数据发送到 PHP 脚本。输入类型文本值似乎没有任何问题,但是当我尝试附加文件时,我收到错误 TypeError: Value does not implement interface FormData.

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

这是表格:

<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 :

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

这是我在 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:值未实现接口(interface) FormData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com