我怎样才能说服 IE 简单地显示应用程序/json 而不是提供下载它? [英] How can I convince IE to simply display application/json rather than offer to download it?
问题描述
在调试使用 AJAX 的 jQuery 应用程序时,我经常需要查看服务返回给浏览器的 json.因此,我会将 JSON 数据的 URL 放入地址栏中.
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.
这对 ASPNET 很好,因为如果出现编码错误,我可以在浏览器中看到 ASPNET 诊断信息:
This is nice with ASPNET because in the event of a coding error, I can see the ASPNET diagostic in the browser:
但是当服务器端代码正常工作并实际返回JSON时,IE提示我下载它,所以我看不到响应.
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.
我可以让 IE 不这样做吗,换句话说,只是像纯文本一样显示它?
我知道如果我将 Content-Type 标头设置为 text/plain
,我可以做到这一点.
I know I could do this if I set the Content-Type header to be text/plain
.
但这特别是 ASPNET MVC 应用程序的上下文,当我在我的一种操作方法上使用 JsonResult 时,它会自动设置响应.我也有点想保留适当的内容类型,而不是仅仅为了支持调试工作而改变它.
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.
推荐答案
我找到了答案.
您可以通过更新注册表将 IE8 配置为在浏览器窗口中显示 application/json.不需要外部工具.我还没有对此进行过广泛的测试,但它适用于 Vista 上的 IE8.
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.
要使用它,请记住,所有关于更新注册表的常见警告都适用.停止 IE.然后,将以下内容剪切并粘贴到一个名为 json-ie.reg
的文件中.
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_ROOTMIMEDatabaseContent Typeapplication/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00
[HKEY_CLASSES_ROOTMIMEDatabaseContent Type ext/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=hex:08,00,00,00
然后双击 .reg 文件.重启IE.修改返回带有 Content-Type: application/json
或 Content-Type: text/json
的文档的 URL 时,您获得的新行为如下所示:
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:
它的作用是什么,为什么有效:
What it does, why it works:
25336920-03F9-11cf-8FD0-00AA00686F13
是原地浏览"操作的 CLSID.基本上这个注册表项告诉 IE,对于具有 mime 类型的 application/json 的文档,只需在适当的位置查看它.这不会影响通过 标签或通过 XHR 等下载的任何应用程序/json 文档.
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.
CLSID 和 Encoding 键获得用于 image/gif
、image/jpeg
和 text/html
的相同值.
The CLSID and Encoding keys get the same values used for image/gif
, image/jpeg
, and text/html
.
这个提示来自 本网站,以及来自微软的文章Handling MIMEInternet Explorer 中的类型.
This hint came from this site, and from Microsoft's article Handling MIME Types in Internet Explorer .
在 FF 中,您也不需要外部加载项.您可以只使用 view-source:
伪协议.在地址栏中输入这样的 URL:
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
这个伪协议曾经在 IE 中得到支持,直到 WinXP-sp2 之前,微软出于安全原因禁用了它.
This pseudo-protocol used to be supported in IE, also, until WinXP-sp2, when Microsoft disabled it for security reasons.
这篇关于我怎样才能说服 IE 简单地显示应用程序/json 而不是提供下载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!