js 跨域请求的方法

1   jsonp 

原理就是在前端定义一个接收数据的方法 , 把方法名拼在连接的后面  , 然后通过 script 标签的 src 就可以了,其它看代码:

连接其它是服务器的,然后服务器通过传过来的参数拿到相应的数据,,服务器提供动态生成脚本并在脚本是写上数据和传过来的方法名,,,调用 起来就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
   <title></title>
   <script type= "text/javascript" >
   // 得到航班信息查询结果后的回调函数
   var  flightHandler = function (data){
     alert( '你查询的航班结果是:票价 '  + data.price + ' 元,'  + '余票 '  + data.tickets + ' 张。' );
   };
   // 提供jsonp服务的url地址(不管是什么类型的地址,最终生成的返回值都是一段javascript代码)
   // 创建script标签,设置其属性
   var  script = document.createElement( 'script' );
   script.setAttribute( 'src' , url);
   // 把script标签加入head,此时调用开始
   document.getElementsByTagName( 'head' )[0].appendChild(script);
   </script>
</head>
<body>
 
</body>
</html>

然后服务器生成的代码大概是这样

1
2
3
4
5
flightHandler({
   "code" : "CA1998" ,
   "price" : 1780,
   "tickets" : 5
});

直接用 jquery 也可以实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  <html xmlns= "http://www.w3.org/1999/xhtml"  >
  <head>
    <title>Untitled Page</title>
    <script type= "text/javascript"  src=jquery.min.js "></script>
    <script type=" text/javascript ">
    jQuery(document).ready(function(){
     $.ajax({
        type: " get ",
        async: false,
        url: " http: //flightQuery.com/jsonp/flightResult.aspx?code=CA1998",
        dataType: "jsonp ",
        jsonp: " callback ",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
        jsonpCallback:" flightHandler ",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写" ?",jQuery会自动为你处理数据
        success: function (json){
          alert( '您查询到航班信息:票价: '  + json.price + ' 元,余票: '  + json.tickets + ' 张。' );
        },
        error: function (){
          alert( 'fail' );
        }
      });
    });
    </script>
    </head>
  <body>
  </body>
  </html>

2  postMessage

window.postMessage概述

1.html5最常用的API之一,实现两个不同域窗口对象之间的数据通信。

2.在发送数据窗口执行:otherWindow.postMessage(msg,origin)

  • otherWindow:表示接受数据的窗口的window对象,包括iframe的contentWindwohe和通过window.open打开的新窗口。
  • msg表示要发送的数据,包扩字符串和对象(ie9以下不支持,可以利用字符串和json互换)
  • origin表示接收的域名。
3.在接受的窗口监听window的message事件,回掉函数参数接受一个事件对象event,包括的属性有:
  • data:接受的数据
  • origin:发送端的域
  • source:发送端的DOMWindow对象
# 利用window.postMessage在框架之间发送数据
1.在父框架页面index.html发送obj对象给远程服务器的wozien.com/test/b.html,该页面是通过iframe加载的,如下
[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>window.postMessage</title>  
  5.       
  6. </head>  
  7. <body>  
  8.     <iframe id="proxy" src="http://wozien.com/test/b.html" onload = "postMsg()" style="display: none" ></iframe>  
  9.   
  10.     <script type="text/javascript">  
  11.         var obj = {  
  12.             msg: 'this is come from client message!'  
  13.         }  
  14.   
  15.         function postMsg (){  
  16.             var iframe = document.getElementById('proxy');  
  17.             var win = iframe.contentWindow;  
  18.             win.postMessage(obj,'http://wozien.com');  
  19.         }  
  20.           
  21.     </script>  
  22. </body>  
  23. </html>  
2.在远程页面b.html中监听message事件,先通过origin属性判断下数据来源的域是否可信任,加强安全措施。具体代码如下:
[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <script type="text/javascript">  
  6.         window.onmessage = function(e){  
  7.             if(e.origin !== 'http://localhost') return;  
  8.             console.log(e.origin+' '+e.data.msg);  
  9.         }  
  10.     </script>  
  11. </head>  
  12. <body>  
  13.     <p>this is my server</p>  
  14. </body>  
  15. </html>  
3.在控制输出结果:
localhoat this is come from client message
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值