Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

javascript - IE tries to download JSON in ASP. NET MVC 3

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?

After that in Javascript part I try this

 if (responseJSON.success == false) {
                        alert(responseJSON.message);
                        cancel();
                    }

But IE doesn't show alert anyway. It brings me save dialog.

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?

EDIT:

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
                        }
                     }
                   });

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've solved that with this trick

return Json(new { success = false, message = ex.Message }, "text/html");

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...