我正在尝试对图像进行发布请求,但是当我将其添加为参数时,却不断出现以下错误Uncaught TypeError: Illegal invocation
。我已经尝试过和.serialize()
,但是似乎没有任何效果。
var description = $('#description').val();
var title = $('#title').val();
var fileInput = $("#image")[0];
var file = fileInput.files[0];
var image = new FormData();
image.append('image', file);
$.ajax({
type: 'POST',
url: 'insert.php',
data: {
desc: description,
title: title,
image:image,
longitude: currentMarker.lng(),
longitude: currentMarker.lat(),
},
success: function (answer) {
}
})
您不能放入FormData
要发送的参数之一,它必须是整个data
值。所以应该是:
var image = new FormData();
image.append('image', file);
image.append('desc', description);
image.append('title', title);
image.append('longitude', currentMarker.lng());
image.append('latitude', currentMarker.lat());
$.ajax({
type: 'POST',
url: 'insert.php',
data: image,
processData: false,
contentType: false,
success: function(answer) {
...
}
});
发送时FormData
,您需要告诉jQuery不要尝试使用将数据参数从对象转换为URL编码的字符串processData: false
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句