我尝试在之前创建 FormData 的 Jquery 中使用 Ajax 发送文件。这是我的代码:
var inputFileImage = document.getElementById('uploadImage');
var file = inputFileImage.files[0];
var data = new FormData();
data.append('archivo',file);
jQuery.ajax({
url: '/services/rpc.php',
type: 'post',
data: {functionName: 'saveProfileImage', data : data},
contentType:false,
processData:false,
})
.done(function(response) {
alert(response);
}.bind(this))
.fail(function(response) {
alert('Sorry, there was an unexpected error, please try again later.\n\nInformation about the error:\n'+response);
});
这个ajax总是转到fail
函数,如果我将processData
更改为true
返回我的另一个错误,说Error :类型错误:在未实现 FormData 接口(interface)的对象上调用了“append”。
感谢您的帮助!
最佳答案
必须关闭 processData 才能发送二进制数据。 FormData 元素完全用作二进制数据,而 data:{}
需要进行处理。在这种情况下,必须将附加参数附加到 formData!
var data = new FormData();
data.append('archivo',file);
data.append('functionName','saveProfileImage');
jQuery.ajax({
url: '/services/rpc.php',
type: 'post',
data: data,
contentType:false,
processData:false,
});
关于javascript - Ajax和FormData发送文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401583/