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