zoukankan      html  css  js  c++  java
  • win下如何解决在chrome的同源访问问题

    引子本来是想验证如果在网页中包含多个框架,那么就会存在两个以上的不同全局环境,如果从一个框架引用另一个框架的数据比如数组a,那么用 instanceof 判断这个数组a是不是另个框架Array的实例,于是html代码如下

    index.html:

     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
     2 <html>
     3 <head>
     4 <title> New Document </title>
     5 </head>
     6 <frameset cols="20%,80%">
     7       <frame src="link.html" name="link" />
     8       <frame src="show.html" name="show" />
     9 </frameset>
    10 </html>

    link.html:

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4   <title></title>
     5 </head>
     6 <body>
     7 <script type="text/javascript">
     8   var arrOrder=[];
     9 </script>
    10 </body>
    11 </html>

    show.html:

    1 <!DOCTYPE html>
    2 <html>
    3 <head>
    4   <title></title>
    5 </head>
    6 <body>
    7 <button onclick="console.log(self.parent.link.arrOrder instanceof Array)">click</button>
    8 </body>
    9 </html>

    效果图:

    在firefox和IE的控制台下可以正常输出false(验证了用instanceof判断一个对象是否为数组的弊端)

    可是chrome控制台下输出:Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

    这是chrome同源策略引发的问题,而且不只是iframe的相互访问时会出现,当需要通过ajax或者get和post方法从本地加载文件或者跨域访问时也会遇到该问题,不然就不会有jsonp的出现了。

    解决方案stackoverflow大法好

    http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome

    我这里只引用一下win平台下的解决方案,linux,OSX平台下可以参考链接里哈

    For Windows go into the command prompt and go into the folder where Chrome.exe is and type

    chrome.exe --disable-web-security

    That should disable the same origin policy and allow you to access local files.

    tip:Make sure you have closed all chrome processes, then try again. Chrome will issue a warning header if you have done it correctly: "You are using an unsupported command-line flag: --disable-web-security. Stability and security will suffer"

    具体做法就是右键浏览器的快捷方式,添加箭头所指的内容然后保存,然后重启浏览器就ok,不过chrome会提示你“你使用的是不受支持的命令行标记:--disabled-web-security,稳定性和安全性会有所下降”,不用管它测试你的项目就好。

    总结关于跨域请求解决的方案不止这一种。有兴趣的可以访问http://slashlook.com/articles_20140523.html

    chrome还有一个插件神器专门解决ajax请求中的跨域问题:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

  • 相关阅读:
    Linux 查看CPU个数和磁盘个数
    springboot 文件上传大小配置
    Netty(一):初识Netty
    Java 8里 Stream和parallelStream的区别
    Logstash filter 的使用
    logstash过滤器插件filter详解及实例
    Linux下如何不停止服务,清空nohup.out文件
    logstash收集Nginx日志,转换为JSON格式
    Logstash add_field 参数应用
    Logstash处理json格式日志文件的三种方法
  • 原文地址:https://www.cnblogs.com/venoral/p/5232676.html
Copyright © 2011-2022 走看看