jQuery 我怎样才能说服 IE 简单地显示 application/json 而不是提供下载它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2483771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:36:19  来源:igfitidea点击:

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

jqueryasp.net-mvcajaxasp.net-ajaxinternet-explorer

提问by Cheeso

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.

在调试使用 AJAX 的 jQuery 应用程序时,我经常需要查看服务返回给浏览器的 json。所以我将把 JSON 数据的 URL 放到地址栏中。

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

这对 ASPNET 很好,因为如果出现编码错误,我可以在浏览器中看到 ASPNET 诊断信息:

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.

但是当服务器端代码正常工作并实际返回JSON时,IE提示我下载它,所以我看不到响应。

alt text

替代文字

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

我可以让 IE 不这样做,换句话说,只是像纯文本一样显示它吗?

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

我知道如果我将 Content-Type 标头设置为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.

但这特别是 ASPNET MVC 应用程序的上下文,当我在我的一种操作方法上使用 JsonResult 时,它会自动设置响应。我也有点想保留适当的内容类型,而不是仅仅为了支持调试工作而改变它。

回答by Cheeso

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.

您可以通过更新注册表将 IE8 配置为在浏览器窗口中显示 application/json。不需要外部工具。我还没有对此进行过广泛的测试,但它适用于 Vista 上的 IE8。

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.

要使用它,请记住,所有关于更新注册表的常见警告都适用。停止 IE。然后,将以下内容剪切并粘贴到一个文件中,名称为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/jsonor Content-Type: text/jsonis like this:

然后双击 .reg 文件。重启IE。对返回带有Content-Type: application/json或的文档的 URL 发痒时,您获得的新行为Content-Type: text/json如下所示:

alt text

替代文字

What it does, why it works:

它的作用是什么,为什么有效:

The 25336920-03F9-11cf-8FD0-00AA00686F13is 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.

25336920-03F9-11cf-8FD0-00AA00686F13是“浏览到位”行动的CLSID。基本上这个注册表项告诉 IE 对于具有 mime 类型的 application/json 的文档,只需在适当的位置查看它。这不会影响通过<script>标签或 XHR 等下载的任何应用程序/json 文档。

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

CLSID和编码密钥获得用于相同的价值观image/gifimage/jpegtext/html

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

这个提示来自这个站点,以及微软的文章在 Internet Explorer 中处理 MIME 类型



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:

在 FF 中,您也不需要外部加载项。您可以只使用view-source:伪协议。在地址栏中输入这样的 URL:

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.

这个伪协议曾经在 IE 中得到支持,直到 WinXP-sp2 之前,微软出于安全原因禁用了它。

回答by Fillipe Silva

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

我有一个类似的问题。我正在使用“$.GetJSON”jQuery,一切都在 Firefox 和 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.

但它在 IE 中不起作用。所以我尝试直接访问json的URL,但是在IE中它询问我是否要下载文件。

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:

但是当发出请求的页面收到此 json 时,在 IE 中,您必须指定 SAME CONTENT-TYPE,在我的情况下是:

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

hugs

拥抱

回答by Tomasz Maj

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.

只需将其保存为 json.reg 文件,然后运行以修改您的注册表。

回答by Porschiey

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.

如果您可以让 IE 将 JSON 打开到记事本中,您可以将系统的 .json 文件的默认程序更改为记事本。

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

为此,请创建或查找 .json 文件,单击鼠标右键,然后选择“打开方式”或“选择默认程序”。

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.

如果您偶然想要使用 Internet Explorer,但您的 IT 公司不允许您编辑注册表,这可能会派上用场。否则,我推荐上述答案。

回答by Chetan Sastry

I use Fiddlerwith JSONViewerplugin to inspect JSON. I don't think it is possible to make IE behave without fiddling with registry perhaps. Here'ssome information.

我使用带有JSONViewer插件的Fiddler来检查 JSON。我不认为有可能让 IE 行为而不摆弄注册表。这里有一些信息。

回答by user1632922

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

更改 IE 的 JSON mime-type 设置将影响 IE 处理所有JSON 响应的方式。

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.

将 mime-type 标头更改为 text/html 将有效地告诉任何浏览器您返回的 JSON 响应不是 JSON 而是纯文本。

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.

相反,您可能希望使用像上面提到的 Fiddler 或任何其他网络流量检查器代理这样的插件或工具,您可以在其中选择每次如何处理 JSON 响应。

回答by Darin Dimitrov

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

FireFox + FireBug 非常适合这个目的。对于 IE,有一个我从未使用过并打算使用的开发人员工具栏,因此我无法提供太多反馈。

回答by Kintar

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.

我只是在 XMLHttpRequest 上遇到了同样的问题。该网站在 Chrome 和 FF 以及数十种生产中的 Internet Explorer 浏览器中完美运行。这台机器(当然,我们公司正在设置为演示机器的机器)决定将提示保存对 ajax 请求的 json 响应。

The accepted regedit solution below fixed it. Thanks.

下面接受的 regedit 解决方案修复了它。谢谢。

回答by asgerhallas

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

您可以在 Fiddler 中看到响应:http: //www.fiddler2.com/fiddler2/

That's nice tool for such things!

这是处理此类事情的好工具!