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

由于同源策略的限制,Javascript存在跨域通信的問題,典型的跨域問題有iframe與父級的通信等。
常規(guī)的幾種解決方法:
(1) document.domain+iframe;
(2) 動態(tài)創(chuàng)建script;
(3) iframe+location.hash;
(4) flash。
這里不細說這幾種方法,記錄的是HTML5的window.postMessage。
postMessage兼容IE8+、Firefox、Opera、Safari、Chrome。
需要兩個異域的服務器來做測試,當然也可以用本地和線上服務器作為兩個異域的服務器。
假如使用phonegap開發(fā),就可以將請求文件安裝在客戶端,然后動態(tài)請求服務器的數(shù)據(jù)處理,獲得并顯示數(shù)據(jù)。這樣就可以用任意Web開發(fā)語言及方法來開發(fā)phonegap App所需的后臺。
1. postMessage的用法
postMessage是HTML5為解決js跨域問題而引入的新的API,允許多個iframe/window跨域通信。
假設有結(jié)構(gòu)如下:
- test.html<section id="wrapper">
- <header>
- <h1>postMessage (跨域)</h1>
- </header>
- <article>
- <form>
- <p>
- <label for="message">給iframe發(fā)一個信息:
- </label>
- <input type="text" name="message" value="son" id="message"/>
- <input type="submit"/>
- </p>
- </form>
- <h4>目標iframe傳來的信息:</h4>
- <p id="test">暫無信息</p>
- <iframe id="iframe"
- src="http://xiebiji.com/works/postmessage/iframe.html">
- </iframe>
- </article>
- </section>
iframe.html
- <strong>iframe指向xiebiji.com</strong><form> <p>
- <label for="message">給父窗口發(fā)個信息:</label>
- <input type="text" name="message" value="dad" id="message"/>
- <input type="submit"/> </p></form>
- <p id="test">暫無信息。</p>下面是test.html里的Javascript代碼(發(fā)送數(shù)據(jù)):var win = document.getElementById("iframe").contentWindow;document.querySelector('form').onsubmit=function(e){
- win.postMessage(document.getElementById("message").value,"*");
- if (e.preventDefault)
- e.preventDefault();
- e.returnValue = false;
- }
關(guān)鍵代碼就一句:
- win.postMessage(document.getElementById("message").value,"*");
postMessage是通信對象的一個方法,所以向iframe通信,就是iframe對象調(diào)用postMessage方法。postMessage有兩個參數(shù),缺一不可。第一個參數(shù)是要傳遞的數(shù)據(jù),第二個參數(shù)是允許通信的域,“*”代表不對訪問的域進行判斷,可理解為允許所有域的通信。
然后是iframe.html里偵聽接收數(shù)據(jù)的代碼:
- var parentwin = window.parent;window.onmessage=function(e){
- document.getElementById("test").innerHTML = e.origin + "say:" + e.data;
- parentwin.postMessage('HI!你給我發(fā)了"<span>'+e.data+'"</span>。',"*");};
很簡單,相信一看就懂了。e.data包含傳送過來的數(shù)據(jù),e.origin指代源域。
然后iframe.html也給test.html發(fā)送回應的數(shù)據(jù),test.html接收數(shù)據(jù)。代碼雷同,就不貼代碼了。
2. Ajax跨域請求
基于以上的跨域通信,只要將Ajax代碼放在iframe.html里的onmessage處理函數(shù)里頭,將test.html用postMessage傳過來的數(shù)據(jù)作為參數(shù)發(fā)送請求,再將返回的數(shù)據(jù)用postMessage傳給test.html。這樣就實現(xiàn)了跨域的Ajax請求。其實是很簡單的東西。
貼個示例代碼吧,但跟以上的代碼無關(guān)。
- (function(){ //獲取跨域數(shù)據(jù) window.onmessage = function(e){
- var url = "http://yangzebo.com/demo/noforget/test.php?msg=" + e.data;
- //Ajax var xhr = getXHR();
- if(xhr){
- xhr.open("GET",url,true);
- xhr.onreadystatechange = changeHandle;
- xhr.send(null); }else{
- alert("不支持Ajax"); }
- function changeHandle(){//返回處理
- if(xhr.readyState == 4){
- var parentwin = window.parent;
- parentwin.postMessage(xhr.responseText,"*");//跨域發(fā)送數(shù)據(jù)
- } }
- function getXHR(){//獲取XMLHttpRequest
- var xhr_temp; if(window.XMLHttpRequest){
- xhr_temp = new XMLHttpRequest();
- }else if(window.ActiveXObject){
- xhr_temp = new ActiveXObject("Microsoft.XMLHTTP");
- }else{
- xhr_temp = null;
- }
- return xhr_temp;
- } };})();
然后給個不好看的Demo。
有興趣代碼請求啥的自個用開發(fā)人員工具看吧,“zebo男”是從數(shù)據(jù)庫讀出來的,“my msg”是sendAndReceive.html發(fā)給test.php的Ajax請求的參數(shù),通過test.php和iframeforAjax.html回傳到sendAndReceive.html。
3. 提示
要獲取iframe的contentWindow才能調(diào)用postMessage。
postMessage一定要在iframe加載完成之后調(diào)用才可以正常運行。(這個耗了我好長時間)
相關(guān)文章
- 這篇文章主要介紹了詳解HTML5 window.postMessage與跨域,非常具有實用價值,需要的朋友可以參考下2017-05-11
- 這篇文章主要介紹了html post請求之a(chǎn)標簽的兩種用法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-05-12