您现在的位置: 365建站网 > 365文章 > JSON跨域请求解决方法总结

JSON跨域请求解决方法总结

文章来源:365jz.com     点击数:435    更新时间:2018-06-05 22:21   参与评论

在这里,对跨域的一些方法做个总结。由于浏览器的同源策略,不同域名、不同端口、不同协议都会构成跨域;但在实际的业务中,很多场景需要进行跨域传递信息,这样就催生出多种跨域方法。

1. 具备src的标签

  • 原理:所有具有 src 属性的HTML标签都是可以跨域的

在浏览器中, <script> 、 <img> 、 <iframe> 和 <link> 这几个标签是可以加载跨域(非同源)的资源的,并且加载的方式其实相当于一次普通的GET请求,唯一不同的是,为了安全起见,浏览器不允许这种方式下对加载到的资源的读写操作,而只能使用标签本身应当具备的能力(比如脚本执行、样式应用等等)。

2. JSONP跨域

  • 原理: <script> 是可以跨域的,而且在跨域脚本中可以直接回调当前脚本的函数

script标签是可以加载异域的JavaScript并执行的,通过预先设定好的callback函数来实现和母页面的交互。它有一个大名,叫做JSONP跨域,JSONP是JSON with Padding的略称。它是一个非官方的协议,明明是加载script,为啥和JSON扯上关系呢?原来就是这个callback函数,对它的使用有一个典型的方式,就是通过JSON来传参,即将JSON数据填充进回调函数,这就是JSONP的JSON+Padding的含义。JSONP只支持GET请求。

前端代码:

</>code

  1. <script type="text/javascript">
  2.  function dosomething(jsondata){
  3.  //处理获得的json数据
  4.  }
  5. </script>
  6. <script src="http://exampl.com/data.php?callback=dosomething"></script>

QQ截图20130613230631

后台代码:

</>code

  1. <?php
  2. $callback = $_GET['callback'];//得到回调函数名
  3. $data = array('a','b','c');//要返回的数据
  4. echo $callback.'('.json_encode($data).')';//输出
  5. ?>

QQ截图20130613230631


3. 跨域资源共享(CORS)

  • 原理:服务器设置Access-Control-Allow-Origin HTTP响应头之后,浏览器将会允许跨域请求

CORS是HTML5标准提出的跨域资源共享(Cross Origin Resource Share),支持GET、POST等所有HTTP请求。CORS需要服务器端设置 Access-Control-Allow-Origin 头,否则浏览器会因为安全策略拦截返回的信息。

</>code

  1. Access-Control-Allow-Origin: *              # 允许所有域名访问,或者
  2. Access-Control-Allow-Origin: http://a.com   # 只允许所有域名访问

windows中iis设置方法:

Access-Control-Allow-Origin

*


123.png


CORS又分为简单跨域和非简单跨域请求,有关CORS的详细介绍请看 跨域资源共享 CORS 详解 ,里面讲解的非常详细。

4. document.domain

  • 原理:相同主域名不同子域名下的页面,可以设置document.domain让它们同域

我们只需要在跨域的两个页面中设置document.domain就可以了。修改document.domain的方法只适用于不同子域的框架间的交互,要载入iframe页面。

例如:1. 在页面 http://a.example.com/a.html 设置document.domain

</>code

  1. <iframe id = "iframe" src="http://b.example.com/b.html" onload = "test()"></iframe>
  2. <script type="text/javascript">
  3.  document.domain = 'example.com';//设置成主域
  4.  function test(){
  5.  alert(document.getElementById('iframe').contentWindow);//contentWindow 可取得子窗口的 window 对象
  6.  }
  7. </script>

QQ截图20130613230631

2、在页面http:// b.example.com/b.html 中设置document.domain

</>code

  1. <script type="text/javascript">
  2.  document.domain = 'example.com';//在iframe载入这个页面也设置document.domain,使之与主页面的document.domain相同
  3. </script>

QQ截图20130613230631

5. window.name

  • 原理:window对象有个name属性,该属性有个特征:即在一个窗口(window)的生命周期内,窗口载入的所有的页面都是共享一个window.name的,每个页面对window.name都有读写的权限,window.name是持久存在一个窗口载入过的所有页面中的。

这里有三个页面:

  • sever.com/a.html 数据存放页面

  • agent.com/b.html 数据获取页面

  • agent.com/c.html 空页面,做代理使用

a.html中,设定 window.name 作为需要传递的值

</>code

  1. <script>
  2.  window.name = 'I was there!';
  3.  alert(window.name);
  4. </script>

b.html中,当iframe加载后将iframe的 src 指向同域的 c.html ,这样就可以利用 iframe.contentWindow.name 获取要传递的值了

</>code

  1. <body>
  2.   <script type="text/javascript">
  3.  iframe = document.createElement('iframe');
  4.  iframe.style.display = 'none';
  5.  var state = 0;
  6.  iframe.onload = function() {
  7.  if(state === 1) {
  8.  var data = JSON.parse(iframe.contentWindow.name);
  9.  alert(data);
  10.  iframe.contentWindow.document.write('');
  11.  iframe.contentWindow.close();
  12.  document.body.removeChild(iframe);
  13.  } else if(state === 0) {
  14.  state = 1;
  15.  iframe.contentWindow.location = 'http://agent.com/c.html';
  16.  }
  17.  };
  18.  iframe.src = 'http://sever.com/a.html';
  19.  document.body.appendChild(iframe);
  20.  </script>
  21. </body>

成功获取跨域数据,效果如下:

6. window.postMesage

  • 原理: HTML5新增的postMessage方法,通过postMessage来传递信息,对方可以通过监听message事件来监听信息。可跨主域名及双向跨域。

这里有两个页面:

  1. agent.com/index.html

  2. server.com/remote.html

本地代码index.html

</>code

  1. <body>  
  2.     <iframe id="proxy" src="http://server.com/remote.html" onload = "postMsg()" style="display: none" ></iframe>  
  3.     <script type="text/javascript"> 
  4.  var obj = { 
  5.  msg: 'hello world' 
  6.  } 
  7.  function postMsg (){ 
  8.  var iframe = document.getElementById('proxy'); 
  9.  var win = iframe.contentWindow; 
  10.  win.postMessage(obj,'http://server.com'); 
  11.  } 
  12.  </script>  
  13. </body>

postMessage 的使用方法: otherWindow.postMessage(message, targetOrigin);

  • otherWindow: 指目标窗口,也就是给哪个window发消息,是 window.frames 属性的成员或者由 window.open 方法创建的窗口

  • message: 是要发送的消息,类型为 String、Object (IE8、9 不支持)

  • targetOrigin: 是限定消息接收范围,不限制请使用 ‘*’

server.com上remote.html,监听 message 事件,并检查来源是否是要通信的域。

</>code

  1. <head>
  2.     <title></title>
  3.     <script type="text/javascript">
  4.         window.onmessage = function(e){
  5.             if(e.origin !== 'http://localhost:8088') return;
  6.             alert(e.data.msg+" from "+e.origin);
  7.         }
  8.     </script>
  9. </head>

7. location.hash

原理:

  • 这个办法比较绕,但是可以解决完全跨域情况下的脚步置换问题。原理是利用location.hash来进行传值。www.a.com下的a.html想和www.b.com下的b.html通信(在a.html中动态创建一个b.html的iframe来发送请求)

  • 但是由于“同源策略”的限制他们无法进行交流(b.html无法返回数据),于是就找个中间人:www.a.com下的c.html(注意是www.a.com下的)。

  • b.html将数据传给c.html(b.html中创建c.html的iframe),由于c.html和a.html同源,于是可通过c.html将返回的数据传回给a.html,从而达到跨域的效果。

a.html代码如下:

</>code

  1. <script>
  2. function startRequest(){ 
  3.  var ifr = document.createElement('iframe'); 
  4.  ifr.style.display = 'none'; 
  5.  ifr.src = 'http://www.b.com/b.html#sayHi'; //传递的location.hash 
  6.  document.body.appendChild(ifr); 
  7. function checkHash() { 
  8.  try { 
  9.  var data = location.hash ? location.hash.substring(1) : ''; 
  10.  if (console.log) { 
  11.  console.log('Now the data is '+data); 
  12.  } 
  13.  } catch(e) {}; 
  14. setInterval(checkHash, 2000); 
  15. window.onload = startRequest;
  16. </script>

b.html代码如下:

</>code

  1. <script>
  2. function checkHash(){
  3.  var data = '';
  4.  //模拟一个简单的参数处理操作
  5.  switch(location.hash){
  6.  case '#sayHello': data = 'HelloWorld';break;
  7.  case '#sayHi': data = 'HiWorld';break;
  8.  default: break;
  9.  }
  10.  data && callBack('#'+data);
  11. }
  12. function callBack(hash){
  13.  // ie、chrome的安全机制无法修改parent.location.hash,所以要利用一个中间的www.a.com域下的代理iframe
  14.  var proxy = document.createElement('iframe');
  15.  proxy.style.display = 'none';
  16.  proxy.src = 'http://localhost:8088/proxy.html'+hash; // 注意该文件在"www.a.com"域下
  17.  document.body.appendChild(proxy);
  18. }
  19. window.onload = checkHash;
  20. </script>

由于两个页面不在同一个域下,IE、Chrome不允许修改parent.location.hash的值,所以要借助于a.com域名下的一个代理iframe,这里有一个a.com下的代理文件c.html。Firefox可以修改。

c.html代码如下:

</>code

  1. <script>parent.parent.location.hash = self.location.hash.substring(1); </script>

直接访问a.html,a.html向b.html发送的消息为”sayHi”;b.html通过消息判断返回了”HiWorld”,并通过c.html改变了location.hash的值

8. flash URLLoader

flash有自己的一套安全策略,服务器可以通过crossdomain.xml文件来声明能被哪些域的SWF文件访问,SWF也可以通过API来确定自身能被哪些域的SWF加载。当跨域访问资源时,例如从域baidu.com请求域google.com上的数据,我们可以借助flash来发送HTTP请求。首先,修改域google.com上的crossdomain.xml(一般存放在根目录,如果没有需要手动创建) ,把baidu.com加入到白名单。其次,通过Flash URLLoader发送HTTP请求,最后,通过Flash API把响应结果传递给JavaScript。Flash URLLoader是一种很普遍的跨域解决方案,不过需要支持iOS的话,这个方案就不可行了。

小结

总的来说,常见的跨域方法如上述。在不同的业务场景下,各有适合的跨域方式。跨域解决了一些资源共享、信息交互的难题,但是有的跨域方式可能会带来安全问题,如jsonp可导致水坑攻击,等标签会被用来进行xss或csrf攻击。所以,在应用跨域的场景,需要格外注意安全问题。


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (435人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号