關於ajax跨域解讀

來源:互聯網
上載者:User

標籤:com   .post   支援   play   服務   on()   example   之間   頁面   


首先要瞭解何為跨域,(協議、網域名稱、連接埠任意一個不同)的web資源。
如何解決跨域:
1,jsonp 它只支援GET請求而不支援POST等其它類型的HTTP請求;
例如angular 中,
如下使用,$http.jsonp(‘some/trusted/url‘, {jsonpCallbackParam: ‘callback‘})

2,HTML5中window.postMessage,在兩個頁面之間的資料傳遞,不適用於用戶端和服務端的通訊;
3,通過修改document.domain來跨子域,需要對兩個版面設定document.domain;
4,通過設定window.name的方式,比如:www.example.com/a.html 想訪問www.cnblogs.com/data.html的內容
則需要在 data頁面中設定window.name = "a.html頁面想要擷取的資料";
在a.html中寫:
寫一個iframe
<iframe id = "proxy" src="http://www.cnblogs.com/data.html" style="display:none" onload = "getData()"></iframe>
<script>
function getData(){
var iframe=document.getElementById("proxy");
iframe.onload = function(){
var data = iframe.contentWindow.name;
alert(data);
}
}
</script>

總結來說,第一種方式,只適用於get請求;
第二三四種方式,則需要兩個通訊的頁面同時操作。

 

關於ajax跨域解讀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.