jsonp跨域

來源:互聯網
上載者:User

標籤: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跨域

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.