因爲js同源策略的影響,當在某一域名下請求其餘域名,或者同一域名,不一樣端口下的url時,就會變成不被容許的跨域請求。
那這個時候一般怎麼解決呢,對此菜鳥光頭我稍做了整理:
1.JavaScript
在原生js(沒有jQuery和ajax支持)的狀況下,一般客戶端代碼是這樣的(我假設是在localhost:8080的端口下的http://localhost:8080/webs/i.mediapower.mobi/wutao/index.html頁面的body標籤下面加入如下代碼):php
<script> var xhr = new XMLHttpRequest(); xhr.open("get", "http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { console.log(xhr.responseText); } }; xhr.send(null); </script>
保存,瀏覽器打開http://localhost:8080/webs/i.mediapower.mobi/wutao/index.html,而且打開console控制檯:html
瀏覽器很無情的給你彈出一個同源限制的錯誤,意思就是你沒法跨域請求url的數據。
那麼,我先採起第一種策略,運用html中的script標籤,插入js腳本:
(1)經過script標籤引用,寫死你須要的src的url地址,好比:jquery
<script> var callbackfunction = function(data) { console.log('我是跨域請求來的數據-->' + data.name); }; </script> <script src="http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js?callback=callbackfunction"></script>
這裏我定義一個callbackfunction的函數,而後用script標籤的src屬性跨域請求數據,那麼,test.js的內容通過約定,須要寫成這樣:
callbackfunction({"name":"wwwwwwwwwwww"});
保存,打開index.html並刷新:web
(2)你也能夠動態的加入script標籤,讓html解析的時候,動態的加載script腳本,並請求遠端數據:ajax
<script> var callbackfunction = function(data) { console.log('我是跨域請求來的數據-->' + data.name); }; var script = document.createElement('script'), body = document.getElementsByTagName('body'); script.src = 'http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js?callback=callbackfunction'; body[0].appendChild(script); </script>
結果和上面同樣。json
2.jQuery中的$.ajax()跨域
設想,當你想要使用jQuery請求跨域數據的時候,好比(仍是剛纔的index.html):瀏覽器
<script src="js/jquery-1.11.3.js"></script> <script> $(function(){ $.get('http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js',function(data){ console.log(data); }) }) </script>
瀏覽器仍是無情的報錯,由於你這個url是不一樣的域名下的。app
那麼既然jQuery封裝了ajax方法,咱們爲什麼不用,人家封裝好了,你不用,豈不是找罪受麼,代碼以下:async
<script src="js/jquery-1.11.3.js"></script> <script> $(function(){ $.ajax({ async: false, type: "GET", dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'callbackfunction', url: "http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js", data: "", timeout: 3000, contentType: "application/json;utf-8", success: function(msg) { console.log(msg); } }); }) </script>
當你做了這麼多挑逗工做以後,瀏覽器很爽快的給出了反應,表示它很爽,返回給了你一個對象,裏面是遠端不一樣域名下test.js中的數據。
3.postMessage+iframe
postMessage是HTML5中新增長的功能,好比我在本地域名下,http://192.168.1.152:8080/webs/i.mediapower.mobi/wutao/testa.html中的testa.html中:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>testa</title> </head> <script> window.onload = function() { document.getElementById('ifr').contentWindow.postMessage('我是要通過傳遞的數據', 'http://i2.mediapower.mobi/adpower/vm/Bora/testb.html'); }; </script> <body> <iframe id="ifr" src="http://i2.mediapower.mobi/adpower/vm/Bora/testb.html"></iframe> </body> </html>
此時,我遠端的testb.html裏面的內容應該是這樣:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>testb</title> </head> <script> window.addEventListener('message', function(event) { // 經過origin屬性判斷消息來源地址 if (event.origin === 'http://192.168.1.152:8080') { alert(event.data); } }, false); </script> <body> 123 </body> </html>
保存代碼,打開本地testa.html,訪問遠端的testb.html
總結了一下,jQuery仍是很是的好用的,基本上js能幹的事情,jQuery都能很是快速而且高效的完成,固然,原生js也能解決不少事情,而HTML5的新功能也很是強大,這幾種方法,我仍是首推jQuery。
以上就是爲你們分享的3種經常使用的js跨域請求數據的方法,但願對你們的學習有所幫助。