我怎样才能说服 IE 简单地显示应用程序/json 而不是提供下载它? [英] How can I convince IE to simply display application/json rather than offer to download it?

查看:26
本文介绍了我怎样才能说服 IE 简单地显示应用程序/json 而不是提供下载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试使用 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/jsonContent-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 的文档,只需在适当的位置查看它.这不会影响通过

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆