32

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 !

smehnawal
  • 553
  • 1
  • 7
  • 16

2 Answers2

46

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");
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 7
    The correct/safest content-type is actually "text/plain". If you use text/html and return HTML as a value of one of your JSON properties, IE8 and earlier tend to do odd things to the response content. – Ray Nicholus Dec 19 '12 at 04:42
  • When using "text/html" as the Content Type and using the `UpdateTargetId` property of `AjaxOptions`, if you return Content Type of "text/html", it will replace the UpdateTargetId's content with the Json you return. If the Content Type is "application/json", it works as expected. Is there any way around this? – John Washam Sep 09 '13 at 21:55
  • 1
    @RayNicholus i tried to change the content-type to "text/plain", and ie8 still tries to download it, set to "text/html" is fine. – YuC Dec 19 '13 at 01:36
  • 2
    Returning 'text/plain' works on all browsers for me. Just have jquery expect json on the other end. – Throttlehead Jul 03 '14 at 16:30
  • Could you please also provide a reference (from Microsoft, if possible) to the statement " IE does not know what to do with application/json contents.."? thanks! – Dr. Gianluigi Zane Zanettini May 28 '15 at 08:37
  • @Dr.GianluigiZaneZanettini I do not have a ref from microsoft, but if you search the internet you will see that it has caused a lot of troubles for a lot of developers. See http://stackoverflow.com/questions/17701992/ie-iframe-doesnt-handle-application-json-response-properly and http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do#2492211 and also https://twittercommunity.com/t/json-p-requests-mime-type-to-be-changed-to-application-javascript-instead-of-application-json/7236/2 – Gabriele Petrioli May 28 '15 at 21:09
  • @GabyakaG.Petrioli : this is definitely true (I'm experiencing the issue first-hand) but some official confirmation (if any exists) is important, expecially when developing for enterprise enviroments. – Dr. Gianluigi Zane Zanettini May 29 '15 at 09:12
  • Are you sure this is a good solution? Changing the resoponse type to something it is not just because IE is broken. Is there realy no way to tell IE just to show json as text? – HMR Oct 29 '15 at 03:03
3

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..

Jack
  • 1,319
  • 8
  • 16
  • I have included this file into my html page, but IE still tries to download the json response. Any help? I have webApi 2.0 – Simon Sep 10 '15 at 07:36