我正在尝试用jquery和ajax上传图片。但这里发生了一件奇怪的事。在控制台中记录其显示
TypeError:在未实现接口FormData的对象上调用了“append”。
请告诉我我哪里做错了?
JS脚本
var prosrc=$("#pro_pix img").last().attr("src");
$("#logoform").on('change',function(event){
var postData = new FormData(this);
$("#pro_pix img").last().hide();
$("#pro_pix img").first().show();
event.preventDefault();
$.ajax(
{
url : "/function/pro_pic_upload.php",
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
$("#pro_pix img").last().show();
$("#pro_pix img").first().hide();
$("#pro_pix h6").text(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
});
我的HTML标记
<div class="row">
<!-- left column -->
<div id="pro_pix" class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<img src="template/image/725.GIF" class="avatar img-circle img-thumbnail" style="display:none" alt="avatar">
<img src="<?php echo $rowuser['profile_logo']; ?>" class="avatar img-circle img-thumbnail" alt="avatar">
<h6>Upload a different photo...</h6>
<form role="form" id="logoform" enctype="multipart/form-data">
<input id="logo" name="logo" type="file" class="text-center center-block well well-sm">
</form>
</div>
</div>
发布于 2014-08-20 02:38:14
为了在jquery中使用formdata,您必须设置正确的选项
$.ajax({
url : "/function/pro_pic_upload.php",
type: "POST",
data : postData,
processData: false,
contentType: false,
success:function(data, textStatus, jqXHR){
$("#pro_pix img").last().show();
$("#pro_pix img").first().hide();
$("#pro_pix h6").text(data);
},
error: function(jqXHR, textStatus, errorThrown){
//if fails
}
});
processData (默认值: true)
类型: Boolean
默认情况下,作为对象(从技术上讲,除字符串以外的任何内容)传递给data选项的数据将被处理并转换为查询字符串,以适合默认的内容类型"application/x-www-form-urlencoded“。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。
发布于 2014-08-20 02:38:08
在AJAX中添加以下两个参数来解决您的问题:
processData: false,
contentType: false,
发布于 2019-07-28 04:09:24
添加:
processData: false,
contentType: false,
肯定会对文件有帮助,另外,如果你想把错误或成功的消息传回页面,你会想要使用json来让你的工作更轻松。
示例:
dataType: 'json',
这将有助于解析您的响应。如果没有它,它将抛出错误。
https://stackoverflow.com/questions/25390598
复制相似问题