使用 ajaxFileUpload 上传图片的时候总是报错
Resource interpreted as Document but transferred with MIME type application/json:
意为资源解释为文档,但是(我们)使用了MIME类型 text/json 去传输。
但是后台确实是使用的
header('Content-Type:application/json; charset=utf-8');
js代码也是 dataType : ‘json’ ,后来发现 使用ajaxFileUpload iframe提交的时候
实际上是使用 iframe+form的方式实现异步上传文件。
用Iframe的时候,返回的时候会添加上html和body标签。jquery不能直接解析的话,只能自己解析了。
代码如下:
var str = $(data).find("body").text();//获取返回的字符串
var data = $.parseJSON(str);`
<script>
// 图片上传ajax
function ajaxFileUpload(id) {
$.ajaxFileUpload({
url : EDITABLE_PAGE_URL+'/image_upload',
secureuri : false,
fileElementId : 'file_'+id,
// dataType : 'json',//使用iframe提交的时候 默认是text/html
data : {name:'file_'+id,config_id : {$Request.param.config_id}},
success : function (data, status) {
var str = $(data).find("body").text();//获取返回的字符串
var data = $.parseJSON(str);
if (data.code!=10000) {
layer.msg(data.message);
} else {
$('input[dstype="path_' + id + '"]').val(data.result.file_path);
$('input[dstype="id_' + id + '"]').val(data.result.file_id);
$('img[dstype="file_' + id + '"]').attr('src', data.result.file_path);
}
},
error:function(data, status){
alert('error');
}
});
return false;
}
</script>