IE尝试下载JSON在ASP。 NET MVC 3 [英] IE tries to download JSON in ASP. NET MVC 3
问题描述
我想从我的行动返回JSON后,IE浏览器试图下载它,并给我保存对话框。
我测试了它在Firefox,并且有它工作正常。
I was trying to return Json from my action and after that IE tried to download it and showed me save dialog. I tested it in Firefox, and there it works fine.
return Json(new { success = false, message = ex.Message }, "application/json");
什么是行为的原因,如何解决这个问题?
What is the reason of that behavior and how can I solve that issue?
之后,在Javascript部分我试试这个
After that in Javascript part I try this
if (responseJSON.success == false) {
alert(responseJSON.message);
cancel();
}
不过,IE浏览器不会自动显示警告反正。它带给我保存对话框。
But IE doesn't show alert anyway. It brings me save dialog.
我试图改变应用/ JSON
与text / plain的
和保存对话框消失,但我无法看到的警报呢。我缺少什么?
I tried to change "application/json"
with "text/plain"
and save dialog disappeared, but I am not able to see alert yet. What am I missing?
编辑:
下面是我编造的Javascript,我使用Valums qquploader(前Ajaxupload)用于上传图片
Here is my complect Javascript, I am using Valums qquploader(ex-Ajaxupload) for uploading images
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
}
}
});
我曾与测试警报(成功);
在我的其他部分并转发JSON作为text / plain的
之后,我看到了警报。但是,在那个时候 responseJSON.success!= FALSE
我。你有什么建议有关?
I had tested with alert("success");
in my else part and forwarded json as "text/plain"
and after that I saw the alert. But in that time responseJSON.success != false
for me. Have you any suggestions about that?
推荐答案
我已经解决了这一招。
return Json(new { success = false, message = ex.Message }, "text/html");
和现在的作品。但我任何人解释为什么它的text / html的作品,并没有与应用/ JSON和text / plain的工作。首先是试图下载JSON和第二为JSON场返回未定义的属性。
And now it works. But can me anyone explain why it works with text/html, and didn't work with application/json and text/plain. First is trying to download JSON and second is returning undefined properties for JSON fields.
这篇关于IE尝试下载JSON在ASP。 NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!