浏览器跨域问题
2016-05-20 21:26
441 查看
1.同样是访问同一台服务器上的 同一个 php页面,(http://112.74.100.71:81/test.php)
google chrome 可以得到正确的php脚本执行之后的结构, 而IE 浏览器却
无法的到 php脚本执行之后的结果, 而提示弹出下载 php 文件的页面,这是为什么呢?
2. 之后我又试了一下,两个浏览器同样都访问 info.php 文件(文件内容phpinfo()), 这个时候两个浏览器都能得到 php 脚本执行之后的结果,这是为什么呢?
考虑了一下, test.php 和 info.php 的差别,一个是有跨域,另外一个是没有跨域,所以总结,可能是 IE 浏览器对跨域有严格的限制,然后在 http 的请求头中做了(手脚)处理,不让实现跨域。气死了,还是谷歌好,跨域就跨域!!! 有时间在研究一下具体是什么原因吧~~
------------------------------华丽的分割线------------------------------------------------------------------------------------------
周末两天不知道自己干嘛去了,以后要有点计划安排,不能这么闲散,懒慢惯了。加油
今天上午终于把这个问题大概弄清楚了。
我们的这个test.php 中有这么一句话: header("content-type: application/json"); IE(或者是IE内核)浏览器在接收到这样的http响应头时,不会在浏览器中当成是 HTML页面展示出来,而是弹出一个下载文件的弹出框,让用户下载保存这个文件,这也是 http 文件下载的原理。所以问题就出在这里,跟我们的服务端是没有关系的,我刚开始还以为是服务端的问题呢。但我自己想想,怎么都觉得不应该是服务端的问题,但又不知道具体的原因到底是什么。所以,一些问题,还是应该要去弄明白,遇到问题也要学会探究,要不然的话问题会越积累越多,造成积重难返,这是一种非常不好的习惯。
好了,下面言归正传,给出具体的解答方法。下面是stack-overflow上面的回答,解决了我的问题 ,这里要感谢一下 这个网站,很不错的哦。
http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do
The Question:
The Answer:
<?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 文件下载的原理。所以问题就出在这里,跟我们的服务端是没有关系的,我刚开始还以为是服务端的问题呢。但我自己想想,怎么都觉得不应该是服务端的问题,但又不知道具体的原因到底是什么。所以,一些问题,还是应该要去弄明白,遇到问题也要学会探究,要不然的话问题会越积累越多,造成积重难返,这是一种非常不好的习惯。
好了,下面言归正传,给出具体的解答方法。下面是stack-overflow上面的回答,解决了我的问题 ,这里要感谢一下 这个网站,很不错的哦。
http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do
The Question:
How can I convince IE to simply display application/json rather than offer to download it?
up vote125down votefavorite 51 | 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. This is nice with ASPNET because in the event of a coding error, I can see the ASPNET diagostic in the browser: 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. Can I get IE to NOT do that, in other words, to just display it as if it were plain text? I know I could do this if I set the Content-Type header to be 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. jquery asp.net-mvc ajax asp.net-ajax internet-explorer
| ||||||||||||
|
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/jsonor Content-Type: text/jsonis like this: 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. 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. |
相关文章推荐
- <<的重载
- linux Cent-OS 安装中文输入法
- Box2D C++ 教程-射线投射
- YTU 2345: 后序遍历二叉树
- c++第六次上机实验
- Ubuntu14.04LTS下学习python2.7.6里程
- android消息处理机制之2handler与looper,MessageQueue:的关系
- 饿了么开源项目Hermes:新颖巧妙易用的Android进程间通信IPC框架
- Box2D C++ 教程-传感器
- hdu 1530
- YTU 2346: 中序遍历二叉树
- 一个平凡的开始
- Box2D C++ 教程-碰撞过滤
- Linux下查看MySQL的安装路径
- Word Aurora公式编辑器介绍、配置、快捷键
- lordofpomelo
- LeetCode 48
- Java之arrayList 方法二
- Problem3-1022
- Unsupported configuration attributes: [FILE_UPLOAD]