欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js跨域請(qǐng)求數(shù)據(jù)的3種常用的方法

 更新時(shí)間:2015年12月01日 09:18:26   作者:我的頭很光  
這篇文章主要介紹了js跨域請(qǐng)求數(shù)據(jù)的3種常用的方法,需要的朋友可以參考下

由于js同源策略的影響,當(dāng)在某一域名下請(qǐng)求其他域名,或者同一域名,不同端口下的url時(shí),就會(huì)變成不被允許的跨域請(qǐng)求。
那這個(gè)時(shí)候通常怎么解決呢,對(duì)此菜鳥光頭我稍作了整理:
1.JavaScript
   在原生js(沒(méi)有jQuery和ajax支持)的情況下,通??蛻舳舜a是這樣的(我假設(shè)是在localhost:8080的端口下的http://localhost:8080/webs/i.mediapower.mobi/wutao/index.html頁(yè)面的body標(biāo)簽下面加入以下代碼):

<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>

保存,瀏覽器打開(kāi)http://localhost:8080/webs/i.mediapower.mobi/wutao/index.html,并且打開(kāi)console控制臺(tái):


瀏覽器很無(wú)情的給你彈出一個(gè)同源限制的錯(cuò)誤,意思就是你無(wú)法跨域請(qǐng)求url的數(shù)據(jù)。
那么,我先采取第一種策略,運(yùn)用html中的script標(biāo)簽,插入js腳本:
(1)通過(guò)script標(biāo)簽引用,寫死你需要的src的url地址,比如:

<script>
var callbackfunction = function(data) {
  console.log('我是跨域請(qǐng)求來(lái)的數(shù)據(jù)-->' + data.name);
};
</script>
<script src="http://i2.mediapower.mobi/adpower/vm/Bora/js/test.js?callback=callbackfunction"></script> 

這里我定義一個(gè)callbackfunction的函數(shù),然后用script標(biāo)簽的src屬性跨域請(qǐng)求數(shù)據(jù),那么,test.js的內(nèi)容經(jīng)過(guò)約定,需要寫成這樣:
callbackfunction({"name":"wwwwwwwwwwww"});
保存,打開(kāi)index.html并刷新:


(2)你也可以動(dòng)態(tài)的加入script標(biāo)簽,讓html解析的時(shí)候,動(dòng)態(tài)的加載script腳本,并請(qǐng)求遠(yuǎn)端數(shù)據(jù):

<script>
var callbackfunction = function(data) {
  console.log('我是跨域請(qǐng)求來(lái)的數(shù)據(jù)-->' + 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>

結(jié)果和上面一樣。

 

2.jQuery中的$.ajax()

   設(shè)想,當(dāng)你想要使用jQuery請(qǐng)求跨域數(shù)據(jù)的時(shí)候,比如(還是剛才的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>

瀏覽器還是無(wú)情的報(bào)錯(cuò),因?yàn)槟氵@個(gè)url是不同的域名下的。

那么既然jQuery封裝了ajax方法,我們?yōu)楹尾挥?,人家封裝好了,你不用,豈不是找罪受么,代碼如下:

<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>

當(dāng)你作了這么多挑逗工作之后,瀏覽器很爽快的給出了反應(yīng),表示它很爽,返回給了你一個(gè)對(duì)象,里面是遠(yuǎn)端不同域名下test.js中的數(shù)據(jù)。
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('我是要經(jīng)過(guò)傳遞的數(shù)據(jù)', '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>

此時(shí),我遠(yuǎn)端的testb.html里面的內(nèi)容應(yīng)該是這樣:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>testb</title>
</head>
<script>
window.addEventListener('message', function(event) {
  // 通過(guò)origin屬性判斷消息來(lái)源地址
  if (event.origin === 'http://192.168.1.152:8080') {
    alert(event.data); 
  }
}, false);
</script>
<body>
123
</body>
</html>

保存代碼,打開(kāi)本地testa.html,訪問(wèn)遠(yuǎn)端的testb.html


總結(jié)了一下,jQuery還是非常的好用的,基本上js能干的事情,jQuery都能非??焖俨⑶腋咝У耐瓿?,當(dāng)然,原生js也能解決很多事情,而HTML5的新功能也非常強(qiáng)大,這幾種方法,我還是首推jQuery。

以上就是為大家分享的3種常用的js跨域請(qǐng)求數(shù)據(jù)的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論