zoukankan      html  css  js  c++  java
  • 浏览器跨域问题

    1.同样是访问同一台服务器上的 同一个 php页面,(http://112.74.100.71:81/test.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 文件下载的原理。所以问题就出在这里,跟我们的服务端是没有关系的,我刚开始还以为是服务端的问题呢。但我自己想想,怎么都觉得不应该是服务端的问题,但又不知道具体的原因到底是什么。所以,一些问题,还是应该要去弄明白,遇到问题也要学会探究,要不然的话问题会越积累越多,造成积重难返,这是一种非常不好的习惯。

    好了,下面言归正传,给出具体的解答方法。下面是stack-overflow上面的回答,解决了我的问题 ,这里要感谢一下 这个网站,很不错的哦。

    http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do

    The Question:

    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:

    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.

    alt text

    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.

    shareimprove this question
     
        
    Firefox 3/4 also does this, if you have a machine that hasn't been updated. – Chris S Jan 27 '12 at 11:24
    3  
    I like the usage of "convince"!! – adardesign Feb 27 '13 at 17:11
        
    You can probably convince IE team by voting on this feature request. – vulcan raven May 4 '14 at 10:35

    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_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

    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:

    alt text

    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/gifimage/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.

  • 相关阅读:
    Sublime Text3下使用Python,REPL的安装与快捷键设置方法
    2018最新版本Sublime Text3注册码(仅供测试交流使用)
    Simple website approach using a Headless CMS: Part 3
    Simple website approach using a Headless CMS: Part 2
    Simple Website Approach Using a Headless CMS: Part 1
    Top 19 Headless CMS for Modern Publishers
    Headless CMS
    12位至今仍在世的重要工程师,让我们来认识这些程序界的大前辈
    Linux操作系统(第二版)(RHEL 8/CentOS 8)—内容简介—前言—清华大学出版社—张同光
    List of open source real-time operating systems
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5513479.html
Copyright © 2011-2022 走看看