javascript跨域的方法總結_javascript技巧
這篇文章學習借鑒了一些其他前端同學的文章,自己做了個實作總結
以下的範例所包含的文件皆為http://www.a.com/a.html 、http://www.a.com/c.html 與http://www.b.com/b.html,要做的都是從a.html取得b.html裡的資料
1.JSONP
jsonp是利用script標籤沒有跨域限制的特性,透過在src的url的參數上附加回呼函數名字,然後伺服器接收回呼函數名字並傳回一個包含資料的回呼函數
function doSomething(data) { // 对data处理 } var script = document.createElement("script"); script.src = "http://www.b.com/b.html?callback=doSomething"; document.body.appendChild(script); // 1.生成一个script标签,将其append在body上,向服务器发出请求 // 2.服务器根据 callback 这个参数生成一个包含数据的函数 doSomething({"a", "1"}) // 3.页面事先已声明doSomething函数,此时执行 doSomething(data) 这个函数,获得数据
2.HTML5的postMessage
假設在a.html裡嵌套個,在這兩個頁面互相通信
a.html
window.onload = function() { window.addEventListener("message", function(e) { alert(e.data); }); window.frames[0].postMessage("b data", "http://www.b.com/b.html"); }
b.html
window.onload = function() { window.addEventListener("message", function(e) { alert(e.data); }); window.parent.postMessage("a data", "http://www.a.com/a.html"); }
這樣開啟a頁面就先彈出 a data,再彈出 b data
3.window.name iframe
window.name的原理是利用同一個視窗在不同的頁面共用一個window.name,這個需要在a.com下建立一個代理檔案c.html,使同源後a.html能取得c.html的window.name
a.html
var iframe = document.createElement("iframe"); iframe.src = "http://www.b.com/b.html"; document.body.appendChild(iframe); // 现在a.html里建一个引用b.html的iframe,获得b的数据 var flag = true; iframe.onload = function() { if (flag) { iframe.src = "c.html"; // 判断是第一次载入的话,设置代理c.html使和a.html在同目录同源,这样才能在下面的else取到data flag = false; } else { // 第二次载入由于a和c同源,a可以直接获取c的window.name alert(iframe.contentWindow.name); iframe.contentWindow.close(); document.body.removeChild(iframe); iframe.src = ''; iframe = null; } }
b.html
window.name = "这是 b 页面的数据";
4.window.location.hash iframe
b.html將資料以hash值的方式附加到c.html的url上,在c.html頁面透過location.hash取得資料後傳到a.html(這個範例是傳到a.html的hash上,當然也可以傳到其他地方)
a.html
var iframe = document.createElement("iframe"); iframe.src = "http://www.b.com/b.html"; document.body.appendChild(iframe); // 在a页面引用b function check() { // 设置个定时器不断监控hash的变化,hash一变说明数据传过来了 var hashs = window.location.hash; if (hashs) { clearInterval(time); alert(hashs.substring(1)); } } var time = setInterval(check, 30);
b.html
window.onload = function() { var data = "this is b's data"; var iframe = document.createElement("iframe"); iframe.src = "http://www.a.com/c.html#" + data; document.body.appendChild(iframe); // 将数据附加在c.html的hash上 }
c.html
// 获取自身的hash再传到a.html的hash里,数据传输完毕 parent.parent.location.hash = self.location.hash.substring(1);
5.CORS
CORS是XMLHttpRequest Level 2 裡規定的跨域方式。在支援這個方式的瀏覽器裡,javascript的寫法和不跨域的ajax寫法一模一樣,只要伺服器需要設定Access-Control-Allow-Origin: *
6.document.domain
這種方式適用於主域相同,子域不同,例如http://www.a.com和http://b.a.com
假如這兩個網域下各有a.html 和b.html,
a.html
document.domain = "a.com"; var iframe = document.createElement("iframe"); iframe.src = "http://b.a.com/b.html"; document.body.appendChild(iframe); iframe.onload = function() { console.log(iframe.contentWindow....); // 在这里操作b.html里的元素数据 }
b.html
document.domain = "a.com";
注意:document.domain需要設定成自身或更高一級的父域,且主域必須相同。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
