码迷,mamicode.com
首页 > Web开发 > 详细

jsonp跨域

时间:2017-01-20 14:13:38      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:efault   read   alert   att   属性   www   程序   draw   query   

1.本地jsonp.html

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="http://localhost/Vitaproject/jsonp.js"></script>  
</head>
<body>
 
</body>
</html>

 

 

远程jsonp.js

 

alert("ok")


2.本地jsonp.html

 

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
	<script type="text/javascript">
	//回调函数
	function callback(data) {
		alert(data.message);
	}
    </script>
    <script type="text/javascript" src="http://localhost/Vitaproject/jsonp.js"></script>  
	
	
</head>
<body>
 
</body>
</html>


远程jsonp.js

 

 

//调用callback函数,并以json数据形式作为阐述传递,完成回调
callback({message:"success"}); 

 

3.JS

本地jsonp.html

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
    // 得到航班信息查询结果后的回调函数
    var flightHandler = function(data){
        alert(‘你查询的航班结果是:票价 ‘ + data.price + ‘ 元,‘ + ‘余票 ‘ + data.tickets + ‘ 张。‘);
    };
    // 提供jsonp服务的url地址(不管是什么类型的地址,最终生成的返回值都是一段javascript代码)
    var url = "http://localhost/Vitaproject/flightResult.asp?code=CA1998&callback=flightHandler";
    // 创建script标签,设置其属性
    var script = document.createElement(‘script‘);
    script.setAttribute(‘src‘, url);
    // 把script标签加入head,此时调用开始
    document.getElementsByTagName(‘head‘)[0].appendChild(script);
    </script>
</head>
<body>
 
</body>
</html>

 

 

http://localhost/Vitaproject/flightResult.asp

 

flightHandler({
    "code": "CA1998",
    "price": 1780,
    "tickets": 5
});



 

4.jQjery

本地jsonp.html

 

<!DOCTYPE html>
<html>
 <head>
     <title>Untitled Page</title>
      <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
      <script type="text/javascript">
     jQuery(document).ready(function(){
        $.ajax({
             type: "get",
             async: false,
             url: "http://localhost/Vitaproject/flightResult.asp?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>

 

 

http://localhost/Vitaproject/flightResult.asp

 

flightHandler({
    "code": "CA1998",
    "price": 1780,
    "tickets": 5
});

 

4.

 

<!DOCTYPE html>
<html>
<head>
	<title>Untitled Page</title>
	<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
	<script type="text/javascript">
	jQuery(document).ready(function(){
        $.ajax({
			type: "get",  
            url:"http://m.qqdyw.cn/lottery/draw/key/", 
            //url:"http://www.jibuu.cn/Main/AjaxServer.aspx?Action=GetActListForDefault", 	
            dataType:‘jsonp‘,  
            jsonp:‘callback‘, 
            success:function(result) {  
                console.log(result);             
				alert(result.status)
			},  
             error: function(){  
                 alert(‘fail‘);  
            }  
         });
     });
	 
	</script>
</head>
<body>
</body>
</html>


({"status":0,"award":"\u4eb2\uff0c\u4e0d\u54ed","item":0})

 

jsonp跨域

标签:efault   read   alert   att   属性   www   程序   draw   query   

原文地址:http://www.cnblogs.com/flower46273736/p/6322490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!