Chu*_*ris 12 javascript asp.net-mvc json asp.net-mvc-3
我试图从我的动作返回Json,之后IE试图下载并向我显示保存对话框.我在Firefox中测试过它,它运行正常.
return Json(new { success = false, message = ex.Message }, "application/json");
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?如何解决这个问题?
之后在Javascript部分我试试这个
if (responseJSON.success == false) {
alert(responseJSON.message);
cancel();
}
Run Code Online (Sandbox Code Playgroud)
但IE无论如何都不会显示警报.它带给我保存对话框.
我试图改变"application/json"与"text/plain"和保存对话框消失了,但我无法看到的警报呢.我错过了什么?
编辑:
这是我的complect Javascript,我使用Valums qquploader(ex-Ajaxupload)来上传图像
var uploader = new qq.FileUploader({
element: document.getElementById("image-upload"),
action: '/Home/ImageUpload',
allowedExtensions: ['jpg', 'png', 'gif'],
sizeLimlit: 2048,onComplete: function (id, fileName, responseJSON) {
if (responseJSON.success == false) {
alert(responseJSON.message);
cancel();
}
else {
alert("success");
//some code here
}
}
});
Run Code Online (Sandbox Code Playgroud)
我已经alert("success");在我的其他部分进行了测试并转发了json "text/plain",之后我看到了警报.但在那个时候responseJSON.success != false对我来说.你有什么建议吗?
Chu*_*ris 17
我用这个技巧解决了这个问题
return Json(new { success = false, message = ex.Message }, "text/html");
Run Code Online (Sandbox Code Playgroud)
现在它有效.但我可以解释为什么它适用于text/html,并且不能与application/json和text/plain一起使用.首先是尝试下载JSON,其次是返回JSON字段的未定义属性.
使用上传插件使用iframeIE上传(在9.0上测试)时会出现此问题.
IE设置标题Accept: text/html, , application/xhtml+xml, */*,所以当你回复时Content-type: application/json,它假定它是文件(或者至少这是我在网上找到的唯一解释).
因此,为了规避这一点,你需要设置Content-type: text/html或Content-type: text/plain.
我建议使用ActionFilter; 而不是手动更改内容类型,检测IE和多部分POST并相应地更改内容类型.
| 归档时间: |
|
| 查看次数: |
8111 次 |
| 最近记录: |