本文實例為大家分享了ajax獲取跨域數據的具體代碼,供大家參考,具體內容如下
1.效果圖
2.源碼
<html><head> <script type="text/javascript" src="/js/jquery.min.js"></script> <script type="text/javascript"> function getData(keyword) { var url="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" +keyword; $.ajax({ type: "get", async: false, url: encodeURI(url), dataType: "jsonp", jsonp: "cb", // 后臺定義的回調函數標識符(一般默認為:callback) jsonpCallback: "jsonCallback", // 自定義的jsonp回調函數名稱(默認為jQuery自動生成的隨機函數名) success: function(data,status){ console.log(data); console.log(status); }, error: function(){ alert('fail'); } }); } </script></head><body> <button onclick="getData('我');">百度搜索"我"</button></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選