亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請(qǐng)求的方法

  發(fā)布時(shí)間:2016-04-19 09:34:56   作者:佚名   我要評(píng)論
這篇文章主要介紹了HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請(qǐng)求的方法的相關(guān)資料,需要的朋友可以參考下

由于同源策略的限制,Javascript存在跨域通信的問(wèn)題,典型的跨域問(wèn)題有iframe與父級(jí)的通信等。
 
常規(guī)的幾種解決方法:

(1) document.domain+iframe;
(2) 動(dòng)態(tài)創(chuàng)建script;
(3) iframe+location.hash;
(4) flash。
 
這里不細(xì)說(shuō)這幾種方法,記錄的是HTML5的window.postMessage。
postMessage兼容IE8+、Firefox、Opera、Safari、Chrome。
 
需要兩個(gè)異域的服務(wù)器來(lái)做測(cè)試,當(dāng)然也可以用本地和線(xiàn)上服務(wù)器作為兩個(gè)異域的服務(wù)器。
假如使用phonegap開(kāi)發(fā),就可以將請(qǐng)求文件安裝在客戶(hù)端,然后動(dòng)態(tài)請(qǐng)求服務(wù)器的數(shù)據(jù)處理,獲得并顯示數(shù)據(jù)。這樣就可以用任意Web開(kāi)發(fā)語(yǔ)言及方法來(lái)開(kāi)發(fā)phonegap App所需的后臺(tái)。
 
1.  postMessage的用法
 
postMessage是HTML5為解決js跨域問(wèn)題而引入的新的API,允許多個(gè)iframe/window跨域通信。
 
假設(shè)有結(jié)構(gòu)如下:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. test.html<section id="wrapper">    
  2. <header>      
  3. <h1>postMessage (跨域)</h1>     
  4. </header>    
  5. <article>       
  6. <form>         
  7. <p>           
  8. <label for="message">給iframe發(fā)一個(gè)信息:   
  9. </label>          
  10. <input type="text" name="message" value="son" id="message"/>          
  11. <input type="submit"/>         
  12. </p>       
  13. </form>       
  14. <h4>目標(biāo)iframe傳來(lái)的信息:</h4>       
  15. <p id="test">暫無(wú)信息</p>       
  16. <iframe id="iframe"       
  17. src="http://xiebiji.com/works/postmessage/iframe.html">   
  18. </iframe>     
  19. </article>   
  20. </section>  

iframe.html

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. <strong>iframe指向xiebiji.com</strong><form>  <p>      
  2. <label for="message">給父窗口發(fā)個(gè)信息:</label>       
  3. <input type="text" name="message" value="dad" id="message"/>       
  4. <input type="submit"/>  </p></form>   
  5. <p id="test">暫無(wú)信息。</p>下面是test.html里的Javascript代碼(發(fā)送數(shù)據(jù)):var win = document.getElementById("iframe").contentWindow;document.querySelector('form').onsubmit=function(e){    
  6. win.postMessage(document.getElementById("message").value,"*");     
  7. if (e.preventDefault)      
  8. e.preventDefault();     
  9. e.returnValue = false;   
  10. }   

  關(guān)鍵代碼就一句:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. win.postMessage(document.getElementById("message").value,"*");   

  postMessage是通信對(duì)象的一個(gè)方法,所以向iframe通信,就是iframe對(duì)象調(diào)用postMessage方法。postMessage有兩個(gè)參數(shù),缺一不可。第一個(gè)參數(shù)是要傳遞的數(shù)據(jù),第二個(gè)參數(shù)是允許通信的域,“*”代表不對(duì)訪(fǎng)問(wèn)的域進(jìn)行判斷,可理解為允許所有域的通信。
 
  然后是iframe.html里偵聽(tīng)接收數(shù)據(jù)的代碼:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. var parentwin = window.parent;window.onmessage=function(e){     
  2. document.getElementById("test").innerHTML = e.origin + "say:" + e.data;     
  3. parentwin.postMessage('HI!你給我發(fā)了"<span>'+e.data+'"</span>。',"*");};  

  很簡(jiǎn)單,相信一看就懂了。e.data包含傳送過(guò)來(lái)的數(shù)據(jù),e.origin指代源域。
 
然后iframe.html也給test.html發(fā)送回應(yīng)的數(shù)據(jù),test.html接收數(shù)據(jù)。代碼雷同,就不貼代碼了。

2. Ajax跨域請(qǐng)求
 
基于以上的跨域通信,只要將Ajax代碼放在iframe.html里的onmessage處理函數(shù)里頭,將test.html用postMessage傳過(guò)來(lái)的數(shù)據(jù)作為參數(shù)發(fā)送請(qǐng)求,再將返回的數(shù)據(jù)用postMessage傳給test.html。這樣就實(shí)現(xiàn)了跨域的Ajax請(qǐng)求。其實(shí)是很簡(jiǎn)單的東西。
 
貼個(gè)示例代碼吧,但跟以上的代碼無(wú)關(guān)。

JavaScript Code復(fù)制內(nèi)容到剪貼板
  1. (function(){  //獲取跨域數(shù)據(jù)  window.onmessage = function(e){ 
  2.    var url = "http://yangzebo.com/demo/noforget/test.php?msg=" + e.data;    
  3. //Ajax    var xhr = getXHR();   
  4.  if(xhr){  
  5.     xhr.open("GET",url,true);     
  6.  xhr.onreadystatechange = changeHandle;      
  7. xhr.send(null);    }else{   
  8.   alert("不支持Ajax");    }   
  9.  function changeHandle(){//返回處理    
  10.   if(xhr.readyState == 4){       
  11.  var parentwin = window.parent;      
  12.   parentwin.postMessage(xhr.responseText,"*");//跨域發(fā)送數(shù)據(jù)      
  13. }    } 
  14.    function getXHR(){//獲取XMLHttpRequest     
  15.  var xhr_temp;      if(window.XMLHttpRequest){      
  16.   xhr_temp = new XMLHttpRequest();    
  17.   }else if(window.ActiveXObject){        
  18. xhr_temp = new ActiveXObject("Microsoft.XMLHTTP");   
  19.    }else{      
  20.   xhr_temp = null;    
  21.   }    
  22.   return xhr_temp;   
  23.  }  };})();   

  然后給個(gè)不好看的Demo。
 
有興趣代碼請(qǐng)求啥的自個(gè)用開(kāi)發(fā)人員工具看吧,“zebo男”是從數(shù)據(jù)庫(kù)讀出來(lái)的,“my msg”是sendAndReceive.html發(fā)給test.php的Ajax請(qǐng)求的參數(shù),通過(guò)test.php和iframeforAjax.html回傳到sendAndReceive.html。
 
3. 提示
 
要獲取iframe的contentWindow才能調(diào)用postMessage。
 
postMessage一定要在iframe加載完成之后調(diào)用才可以正常運(yùn)行。(這個(gè)耗了我好長(zhǎng)時(shí)間)

相關(guān)文章

  • 詳解HTML5 window.postMessage與跨域

    這篇文章主要介紹了詳解HTML5 window.postMessage與跨域,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-11
  • html post請(qǐng)求之a(chǎn)標(biāo)簽的兩種用法解析

    這篇文章主要介紹了html post請(qǐng)求之a(chǎn)標(biāo)簽的兩種用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-05-12

最新評(píng)論