Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API IE9 JSON Data “do you want to open or save this file”

First off, please understand that I am not trying to view JSON in IE9. I use Fiddler all the time. I have a third party uploader called Fine Uploader that expects JSON response messages upon successful Ajax file uploads.

Now I understand that IE9 does not have a MIME to display JSON but I am NOT trying to display JSON. I am just returning a JSON response after ajax uploading some files and IE9 attempts to open my ajax response.

Since I intend to have all my Web API responses formatted in JSON do I have to add a custom text/html response content-type for every api call to make my application compatible with IE9? Seriously ridiculous stuff, I can't get over how much time I waste dealing with IE compatibility issues.

like image 312
Brian Ogden Avatar asked Dec 29 '12 03:12

Brian Ogden


People also ask

What is the best way to open JSON files?

Cross-platform to open JSON files: Generally, users can open the JSON file in any text editor as it is a plain text-based file. The Google Chrome and Mozilla Firefox web browsers are cross-platform to open JSON files that are compatible with every operating system (OS).


1 Answers

Although Ray's answer works, this changes the response content type for all Json respones. Ideally, we shouldn't affect the response type for browsers which play nicely.

For an easy way to resolve this (particularly in MVC) see my answer to this question.

In short, the following code does the trick, and can be wrapped into a base controller / helper method if applicable:

if (!Request.AcceptTypes.Contains("application/json"))
    return base.Json(data, "text/plain");
else
    return base.Json(data);

I hope that helps someone else!

like image 103
Chris Avatar answered Oct 24 '22 07:10

Chris