1.同样是访问同一台服务器上的 同一个 php页面



<?php
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');

$rsp = array();
$rsp['data'] = 'hello, github and my personal blog, I am coming!';
$rsp = json_encode($rsp);
echo $rsp;


google chrome 可以得到正确的php脚本执行之后的结构, 而IE 浏览器却

无法的到 php脚本执行之后的结果, 而提示弹出下载 php 文件的页面,这是为什么呢?

 

 

 

2. 之后我又试了一下,两个浏览器同样都访问 info.php 文件(文件内容phpinfo()), 这个时候两个浏览器都能得到 php 脚本执行之后的结果,这是为什么呢?



<?php
phpinfo();
~
~
~


  

考虑了一下, test.php 和 info.php 的差别,一个是有跨域,另外一个是没有跨域,所以总结,可能是 IE 浏览器对跨域有严格的限制,然后在  http 的请求头中做了(手脚)处理,不让实现跨域。气死了,还是谷歌好,跨域就跨域!!! 有时间在研究一下具体是什么原因吧~~

 

------------------------------华丽的分割线------------------------------------------------------------------------------------------

周末两天不知道自己干嘛去了,以后要有点计划安排,不能这么闲散,懒慢惯了。加油

今天上午终于把这个问题大概弄清楚了。

我们的这个test.php 中有这么一句话: header("content-type: application/json");  IE(或者是IE内核)浏览器在接收到这样的http响应头时,不会在浏览器中当成是 HTML页面展示出来,而是弹出一个下载文件的弹出框,让用户下载保存这个文件,这也是 http 文件下载的原理。所以问题就出在这里,跟我们的服务端是没有关系的,我刚开始还以为是服务端的问题呢。但我自己想想,怎么都觉得不应该是服务端的问题,但又不知道具体的原因到底是什么。所以,一些问题,还是应该要去弄明白,遇到问题也要学会探究,要不然的话问题会越积累越多,造成积重难返,这是一种非常不好的习惯。


The Answer:

 


173down voteaccepted




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:

浏览器跨域问题_跨域

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.