163

While debugging jQuery apps that use AJAX, I often have the need to see the json that is being returned by the service to the browser. So I'll drop the URL for the JSON data into the address bar.

This is nice with ASPNET because in the event of a coding error, I can see the ASPNET diagostic in the browser:

alt text

But when the server-side code works correctly and actually returns JSON, IE prompts me to download it, so I can't see the response.

alt text

Can I get IE to NOT do that, in other words, to just display it as if it were plain text?

I know I could do this if I set the Content-Type header to be text/plain.

But this is specifically an the context of an ASPNET MVC app, which sets the response automagically when I use JsonResult on one of my action methods. Also I kinda want to keep the appropriate content-type, and not change it just to support debugging efforts.

Cheeso
  • 189,189
  • 101
  • 473
  • 713

9 Answers9

236

I found the answer.

You can configure IE8 to display application/json in the browser window by updating the registry. There's no need for an external tool. I haven't tested this broadly, but it works with IE8 on Vista.

To use this, remember, all the usual caveats about updating the registry apply. Stop IE. Then, cut and paste the following into a file, by the name of json-ie.reg.

Windows Registry Editor Version 5.00
;
; Tell IE to open JSON documents in the browser.  
; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
;  

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

Then double-click the .reg file. Restart IE. The new behavior you get when tickling a URL that returns a doc with Content-Type: application/json or Content-Type: text/json is like this:

alt text

What it does, why it works:

The 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" action. Basically this registry entry is telling IE that for docs that have a mime type of application/json, just view it in place. This won't affect any application/json documents downloaded via <script> tags, or via XHR, and so on.

The CLSID and Encoding keys get the same values used for image/gif, image/jpeg, and text/html.

This hint came from this site, and from Microsoft's article Handling MIME Types in Internet Explorer .


In FF, you don't need an external add-on either. You can just use the view-source: pseudo-protocol. Enter a URL like this into the address bar:

view-source:http://myserver/MyUrl/That/emits/Application/json

This pseudo-protocol used to be supported in IE, also, until WinXP-sp2, when Microsoft disabled it for security reasons.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • I tried this one on XP but didn't work. @SkySanders solution work. Nevertheless, +1 :) – KaeL Nov 17 '11 at 09:44
  • 13
    If you want to skip creating the registry file, here's a one-liner you can paste into a powershell window: `reg add "HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json" /t REG_SZ /d "{25336920-03F9-11cf-8FD0-00AA00686F13}"; reg add "HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json" /v Encoding /t REG_DWORD /d 0x08000000` – Jason R. Coombs Jan 26 '12 at 19:35
  • 1
    Brilliant, brilliant, brilliant! At last, my IE 8 can behave like a grown-up browser, like Chrome, and display the results of a JSON web call. Many thanks for this .reg script... worked a treat. Brilliant! – Mike Gledhill Jul 01 '12 at 18:03
  • 1
    Is this technique known to work (or NOT work) on IE 9? It's not working for me yet, am I wasting my time trying? – jwfearn Jul 07 '12 at 16:46
  • 1
    Thanks @Cheeso. NOTE: some web APIs return JSON with the Content-type `text/javascript` to also view these in-place in IE (including IE9) use the `.reg` script above but add a third section for `text/javascript`. – jwfearn Jul 09 '12 at 03:10
  • 1
    This cannot be the solution. This would mean I would have to ask the largest part of the world (e.g. IE users) to run some script to fix the registry???? – nathanvda Jul 23 '12 at 11:57
  • 2
    @nathanvda, there is no need to ask the largest part of the world anything. Showing JSON in the browser is something a developer of an AJAX page would want to do. It's completely unnecessary to display JSON in the browser for regular users. – Cheeso Jul 23 '12 at 17:30
  • My bad, I misread the question: i had a form which returned json which failed with the same error. – nathanvda Jul 24 '12 at 08:35
  • 3
    I made the fatal mistake of allowing a friend to borrow my box and he ran this fix. Does anyone know what the original settings are so that I might reverse them? – KevDog Jan 01 '13 at 01:29
  • 1
    @KevDog just delete the application/json and text/json keys entirely, they do not exist by default. – Marcus Pope Jun 04 '13 at 19:45
  • Works fine with IE10/Windows 7 (+1) – Tom Sep 17 '13 at 08:39
  • Not sure why this response has received 80 points?! What about a public facing website visited by thousands or million users running IE? Would you tell each user to go and update their registry so IE displays JSON for them?! There must be a better solution! – Mosh Dec 04 '13 at 01:23
  • 7
    @Mosh, this was mentioned by nathanvda above, the solution isn't so that users of the website can view the JSON, it's simply for a debugging purpose. Read the entire question. – Anthony Shaw Jan 16 '14 at 17:01
  • 1
    If you don't have admin rights, then you can replace `HKEY_CLASSES_ROOT\MIME` with `HKEY_CURRENT_USER\Software\Classes\MIME`. – djKianoosh Sep 12 '14 at 15:25
  • 2
    This is still working in IE11 on win 8.1. Didn't even need to restart the browser. In my case I needed the `text/json` key. – Christopher Feb 07 '15 at 23:30
  • OH OK, I'll just ask the whole internet to do this... **smh** – catbadger Jan 22 '18 at 21:21
15

I had a similar problem. I was using the "$. GetJSON" jQuery and everything worked perfectly in Firefox and Chrome.

But it did not work in IE. So I tried to directly access the URL of json, but in IE it asked if I wanted to download the file.

After much searching I saw that there must be a header in the result with a content-type, in my case, the content-type was:

header("Content-type: text/html; charset=iso-8859-1");

But when the page that made the request receives this json, in IE, you have to be specified SAME CONTENT-TYPE, in my case was:

$.getJSON (
"<? site_url php echo (" ajax / tipoMenu ")?>"
{contentType: 'text / html; charset = utf-8'},
function (result) {

hugs

Phil
  • 1,288
  • 1
  • 10
  • 19
Fillipe Silva
  • 159
  • 1
  • 2
  • This worked for me, no messing with registry required, just change the response header too "text/html". – 64k Mar 25 '12 at 06:32
  • 2
    How does this work in IE7 when making an Ajax call and receives json as a result? I'm not using getJSON but rather a .ajax call. I've used that content type in response and header and it doesnt help at all. Neither did using json2.js. Does anyone have successful implementations of this that work in ie7? IE7 represents more than 30% of our users. Sad but true, and a reg key change isn't an option. Telling a million governmment users to just run a reg hack won't fly. – CD Smith Apr 30 '12 at 23:41
  • I know this is an old question, but it is being repeated, so here's the answer: The original question pertains to DEVELOPERS who are debugging the retrieval of JSON. Modifying the reg is suggested as a step to help those developers. Modyfing the registry is not recommended or suggested for general users. Also: there is NEVER a need for regular users (= a million government users) to view JSON in the browser. – Cheeso Jan 17 '14 at 17:36
  • Just use `text/plain; charset=UTF-8`. No need to text/html. – perryflynn Sep 26 '16 at 09:28
13

Above solution was missing thing, and below code should work in every situation:

Windows Registry Editor Version 5.00
;
; Tell IE to open JSON documents in the browser.  
; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
;  

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/x-json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

[HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00

Just save it file json.reg, and run to modify your registry.

Tomasz Maj
  • 1,541
  • 15
  • 15
  • My work computer is locked down. If you need to command line add these, see this answer. https://stackoverflow.com/questions/13809856/run-reg-command-in-cmd-bat-file/13811568#13811568 Thanks OP. I see json in IE........yay! – granadaCoder Sep 07 '18 at 13:12
2

Changing IE's JSON mime-type settings will effect the way IE treats all JSON responses.

Changing the mime-type header to text/html will effectively tell any browser that the JSON response you are returning is not JSON but plain text.

Neither options are preferable.

Instead you would want to use a plugin or tool like the above mentioned Fiddler or any other network traffic inspector proxy where you can choose each time how to process the JSON response.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 2
    `text/html` does not tell the browser that the response is plain text, but HTML. If you do decide you want plain text, use `text/plain`. Otherwise you can end up with some interesting XSS vulnerabilities. – Michael Mior Jun 19 '13 at 01:55
2

If you are okay with just having IE open the JSON into a notepad, you can change your system's default program for .json files to Notepad.

To do this, create or find a .json file, right mouse click, and select "Open With" or "Choose Default Program."

This might come in handy if you by chance want to use Internet Explorer but your IT company wont let you edit your registry. Otherwise, I recommend the above answers.

Porschiey
  • 1,981
  • 2
  • 17
  • 25
1

I use Fiddler with JSONViewer plugin to inspect JSON. I don't think it is possible to make IE behave without fiddling with registry perhaps. Here's some information.

Chetan S
  • 23,637
  • 2
  • 63
  • 78
0

FireFox + FireBug is very good for this purpose. For IE there's a developer toolbar which I've never used and intend to use so I cannot provide much feedback.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • yep, I know about the FF plugin that allows the display of application/json as regular text. I specifically wanna know about IE, though. – Cheeso Mar 20 '10 at 16:22
  • There's a developer toolbar for IE. By the way here's a nice comparison of different tools for developers : http://elegantcode.com/2009/05/18/firefoxfirebug-vs-ie8-developer-toolbar/ – Darin Dimitrov Mar 20 '10 at 16:25
0

I just had the same issue with an XMLHttpRequest. The site functions flawlessly in Chrome and FF, and in dozens upon dozens of Internet Explorer browsers in production. This ONE machine (the one our company is setting up to be a demo machine, of course) decided that it was going to prompt to save the json response to an ajax request.

The accepted regedit solution below fixed it. Thanks.

Kintar
  • 71
  • 2
-1

You could see the response in Fiddler: http://www.fiddler2.com/fiddler2/

That's nice tool for such things!

asgerhallas
  • 16,890
  • 6
  • 50
  • 68