前端跨域问题常见的几种解决方案

一、同源策略

  所谓同源是指,域名,协议,端口相同。当浏览器运行一个JS脚本时会进行同源检测,如果不同源是不能执行的。

二、方法

  1、document.domain + iframe(变更原)

  在a.name.com/a.html中插入:

 1 document.domain = 'a.com';
 2 
 3 var ifr = document.createElement('iframe');
 4 ifr.src = 'http://b.name.com/b.html';
 5 ifr.display = none;
 6 document.body.appendChild(ifr);
 7 
 8 ifr.onload = function(){
 9     var doc = ifr.contentDocument || ifr.contentWindow.document;
10     //在这里操作doc,也就是b.html
11     ifr.onload = null;
12 };

  在b.name.com/b.html中插入:

document.domain = 'name.com';

 2、动态创建script

  因为script标签不受同源策略的限制

function loadScript(url, func) {
  var head = document.head || document.getElementByTagName('head')[0];
  var script = document.createElement('script');
  script.src = url;
  script.onload = script.onreadystatechange = function(){
    if(!this.readyState || this.readyState=='loaded' || this.readyState=='complete'){
      func();
      script.onload = script.onreadystatechange = null;
    }
  };

  head.insertBefore(script, script[0]);
}
window.baidu = {
  sug: function(data){
    console.log(data);
  }
}
loadScript('https://www.baidu.com',function(){console.log('loaded')});

  3、window.name + iframe

window对象有个name属性,该属性有个特征:即在一个窗口(window)的生命周期内,窗口载入的所有的页面都是共享一个window.name的,每个页面对window.name都有读写的权限,window.name是持久存在一个窗口载入过的所有页面中的,并不会因新页面的载入而进行重置,例如:a.com/a.html

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title></title>
 6         <script>
 7             function getData(){
 8                 //此时window.name已被修改为b.com/b.html页面设置的数据
 9                 var iframe = document.getElementById('proxy');
10                 iframe.onload = function(){
11                     var data = iframe.contentWindow.name;//获取iframe中window.name,也就是b.com/b.html页面设置的数据
12                     alert(data);
13                 }
14                 iframe.src = 'about:block'; //赊着src的目的是为了让iframe与当前页面同源。src被修改后会重新load然后触发上面的onload
15             }
16         </script>
17     </head>
18     <body>
19         <iframe id="proxy" src="b.com/b.html" onload="getData()"></iframe>
20     </body>
21 </html>

4、postMessage(HTML5中的XMLHttpRequest Level 2中的API)

  window.postMessage(message,targetOrigin)  方法是html5新引进的特性,可以使用它来向其它的window对象发送消息,无论这个window对象是属于同源或不同源,目前IE8+、FireFox、Chrome、Opera等浏览器都已经支持window.postMessage方法。

  调用postMessage方法的window对象是指要接收消息的那一个window对象,该方法的第一个参数message为要发送的消息,类型只能为字符串;第二个参数targetOrigin用来限定接收消息的那个window对象所在的域,如果不想限定域,可以使用通配符 *  。

  需要接收消息的window对象,可是通过监听自身的message事件来获取传过来的消息,消息内容储存在该事件对象的data属性中。

  上面所说的向其他window对象发送消息,其实就是指一个页面有几个框架的那种情况,因为每一个框架都有一个window对象。在讨论第二种方法的时候,我们说过,不同域的框架间是可以获取到对方的window对象的,而且也可以使用window.postMessage这个方法。下面看一个简单的示例,有两个页面

  a.com/index.html

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title></title>
 6         <script>
 7             var iframe = document.getElementById('iframe');
 8             iframe.contentWindow.postMessage('我是a.com/index.hmtl的消息', '*');
 9         </script>
10     </head>
11     <body>
12         <iframe id="iframe" src="b.com/index.html"></iframe>
13     </body>
14 </html>

  b.com/index.html

1 <script>
2     window.onmessage = function(e){
3         e = e || event;
4         alert(e.data)
5     }
6 </script>

5、CORS(Cross-Origin Resource Sharing)

  跨源资源共享(CORS)是通过客户端+服务端协作声明的方式来确保请求安全的。服务端会在HTTP请求头中增加一系列HTTP请求参数(例如Access-Control-Allow-Origin等),来限制哪些域的请求和哪些请求类型可以接受,而客户端在发起请求时必须声明自己的源(Orgin),否则服务器将不予处理,如果客户端不作声明,请求甚至会被浏览器直接拦截都到不了服务端。

前端:

 1 function getHello() {
 2     var xhr = new XMLHttpRequest();
 3     xhr.open("post", "https://b.example.com/Test.ashx", true);
 4     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
 5     
 6     xhr.onreadystatechange = function () {
 7         if (xhr.readyState == 4 && xhr.status == 200) {
 8             var responseText = xhr.responseText;
 9             console.info(responseText);
10         }
11     }
12     xhr.send();
13 }

服务端:(https://b.example.com/Test.ashx)

header('Access-Control-Allow-Origin:*')

  *也可以指定具体的来源

6、JOSNP

1 function handleResponse(response){
2     console.log('The responsed data is: '+response.data);
3 }
4 var script = document.createElement('script');
5 script.src = 'http://www.baidu.com/json/?callback=handleResponse';
6 document.body.insertBefore(script, document.body.firstChild);

7、Nginx反向代理

  前端调用的服务 /apis/xxxx/xxxx  和当前页是同源的,nginx来做一个代理到想要的地方,来实现跨域

  nginx.conf 配置一个反向代理路径

location /apis {
    rewrite ^.+apis/?(.*)$ /$1 break;
    include uwsgi_params;
    proxy_pass http://www.baicu.com/xxxx
}
posted @ 2017-05-04 17:52  徐大卷子  阅读(281)  评论(0)    收藏  举报