Json response download in IE(7~10)

I am trying to upload a file and return a json response regarding properties(name, size etc) of the file. It works fine in all browsers except IE.

IE tries to download the JSON as a file !

I have IE10 and testing it on IE7 to 10 by changing browser mode and document mode from the debugger.

I am using asp.net mvc4, the file upload action have HttpPost attribute and i am returning json response using return Json(myObject);

And here are my http headers

Request

Key Value
Request POST /File/UploadFile/ HTTP/1.1
Accept  text/html, application/xhtml+xml, */*
Referer http://localhost:63903/
Accept-Language en-NZ
User-Agent  Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type    multipart/form-data; boundary=---------------------------7dc1e71330526
Accept-Encoding gzip, deflate
Host    localhost:63903
Content-Length  1377002
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache

Response

Key Value
Response    HTTP/1.1 200 OK
Server  ASP.NET Development Server/11.0.0.0
Date    Tue, 18 Dec 2012 23:44:19 GMT
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 4.0
Cache-Control   private
Content-Type    application/json; charset=utf-8
Content-Length  154
Connection  Close

I tried a few suggestions but so far back to square one !

Answers


You will need to return the json as text/html since IE does not know what to do with application/json contents..

return Json(myObject, "text/html");

Not sure but it might work (and it would be more correct if it does) to use text/x-json

return Json(myObject, "text/x-json");

Even though this question is a few months old, I thought I'll add one more suggestion, just in case anyone else is using ASP.NET MVC 3 or 4 and runs into this problem.

In my experience, when IE attempts to download the Json response as a file all you have to do to correct the problem is to add a reference to jquery.unobtrusive to your view.

for example:

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

Once this is in place IE will no longer try to download the json response from a JsonResult controller action. No need to change the response type etc..


Need Your Help

java.io.IOException: Server returned HTTP response code: 403 for URL

java http http-status-code-403

I want to download the mp3 file from url : "http://upload13.music.qzone.soso.com/30671794.mp3", i always got java.io.IOException: Server returned HTTP response code: 403 for URL. But it's ok when o...

using static libraries instead of dynamic libraries in opencv

c++ visual-studio opencv

I have a project in visual studio 2012 which uses opencv dynamic libraries. It compiled, linked and worked well.