How can I convince IE to simply display application/json rather than offer to download it?

前端 未结 9 539
野的像风
野的像风 2020-11-22 14:57

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 da

相关标签:
9条回答
  • 2020-11-22 15:44

    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

    0 讨论(0)
  • 2020-11-22 15:49

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

    That's nice tool for such things!

    0 讨论(0)
  • 2020-11-22 15:53

    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.

    0 讨论(0)
提交回复
热议问题