一:同源策略
1.what's this
所謂同源是指,域名,協議,端口相同。當瀏覽器運行一個JS腳本時會進行同源檢測,如果不同源是不能執行的。
2.源繼承
來自about:blank,javascript:和data:URLs中的內容,繼承了將其載入的文檔所指定的源,因為它們的URL本身未指定任何關于自身源的信息。
3.變更源
變更源可以實現基礎域相同的不同頁面的跨域問題。
如:a.baidu.com/index.html通過iframe引入b.baidu.com/index.html,但是a中的JS是不可以操作b中的內容的,但是可以通過修改源來實現。需要在a和b中都修改domain,即document.domain = 'baicu.com'
注意:document.doamin的修改必須是當前域或者當前域的基礎域,如在a中document.domain = 'b.baidu.com'是報錯的
二:跨域方案
共有幾種解決方案:
(1)document.domain+ iframe
(2)動態創建script
(3)window.name +iframe
(4)window.postMessage
(5)CORS
(6)JSONP
(7)nginx代理
1.document.domain + iframe
這種方式就是上面說的變更源
在a.name.com/a.html中
document.domain = 'a.com';
var ifr = document.createElement('iframe');
ifr.src = 'http://b.name.com/b.html';
ifr.display = none;
document.body.appendChild(ifr);
ifr.onload = function(){
var doc = ifr.contentDocument || ifr.contentWindow.document;
//在這里操作doc,也就是b.html
ifr.onload= null;
};
在b.name.com/b.html中
document.domain = 'name.com';
2.動態創建script
因為script標簽不受同源策略的限制
function loadScript(url, func) {
varhead = document.head || document.getElementByTagName('head')[0];
varscript = document.createElement('script');
script.src = url;
script.onload = script.onreadystatechange = function(){
if(!this.readyState || this.readyState=='loaded' ||this.readyState=='complete'){
func();
script.onload = script.onreadystatechange = null;
}
};
head.insertBefore(script, script[0]);
}
window.baidu = {
sug: function(data){
console.log(data);
}
}
loadScript('https://www.baidu.com',function(){console.log('loaded')});
3.window.name + iframe
window對象有個name屬性,該屬性有個特征:即在一個窗口(window)的生命周期內,窗口載入的所有的頁面都是共享一個window.name的,每個頁面對window.name都有讀寫的權限,window.name是持久存在一個窗口載入過的所有頁面中的,并不會因新頁面的載入而進行重置
a.com/a.html
function getData(){
//此時window.name已被修改為b.com/b.html頁面設置的數據
var iframe =document.getElementById('proxy');
iframe.onload = function(){
var data =iframe.contentWindow.name;//獲取iframe中window.name,也就是b.com/b.html頁面設置的數據
alert(data);
}
iframe.src = 'about:block'; //賒著src的目的是為了讓iframe與當前頁面同源。src被修改后會重新load然后觸發上面的onload
}
4.postMessage(HTML5中的XMLHttpRequest Level 2中的API)
window.postMessage(message,targetOrigin)方法是html5新引進的特性,可以使用它來向其它的window對象發送消息,無論這個window對象是屬于同源或不同源,目前IE8+、FireFox、Chrome、Opera等瀏覽器都已經支持window.postMessage方法。
調用postMessage方法的window對象是指要接收消息的那一個window對象,該方法的第一個參數message為要發送的消息,類型只能為字符串;第二個參數targetOrigin用來限定接收消息的那個window對象所在的域,如果不想限定域,可以使用通配符*。
需要接收消息的window對象,可是通過監聽自身的message事件來獲取傳過來的消息,消息內容儲存在該事件對象的data屬性中。
上面所說的向其他window對象發送消息,其實就是指一個頁面有幾個框架的那種情況,因為每一個框架都有一個window對象。在討論第二種方法的時候,我們說過,不同域的框架間是可以獲取到對方的window對象的,而且也可以使用window.postMessage這個方法。下面看一個簡單的示例,有兩個頁面
a.com/index.html
var iframe = document.getElementById('iframe');
iframe.contentWindow.postMessage('我是a.com/index.hmtl的消息', '*');
b.com/index.html
window.onmessage = function(e){
e = e || event;
alert(e.data)
}
5.CORS(Cross-Origin Resource Sharing)
跨源資源共享(CORS)是通過客戶端+服務端協作聲明的方式來確保請求安全的。服務端會在HTTP請求頭中增加一系列HTTP請求參數(例如Access-Control-Allow-Origin等),來限制哪些域的請求和哪些請求類型可以接受,而客戶端在發起請求時必須聲明自己的源(Orgin),否則服務器將不予處理,如果客戶端不作聲明,請求甚至會被瀏覽器直接攔截都到不了服務端。
前端:
function getHello() {
var xhr = new XMLHttpRequest();
xhr.open("post", "https://b.example.com/Test.ashx",true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var responseText = xhr.responseText;
console.info(responseText);
}
}
xhr.send();
}
服務端:(https://b.example.com/Test.ashx)
header('Access-Control-Allow-Origin:*')
*也可以指定具體的來源
6.JSONP
function handleResponse(response){
console.log('The responsed data is: '+response.data);
}
var script =document.createElement('script');
script.src ='http://www.baidu.com/json/?callback=handleResponse';
document.body.insertBefore(script,document.body.firstChild);
7.Nginx反向代理
前端調用的服務/apis/xxxx/xxxx和當前頁是同源的,nginx來做一個代理到想要的地方,來實現跨域
nginx.conf配置一個反向代理路徑
location /apis {
rewrite ^.+apis/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://www.baicu.com/xxxx
}
好了,就總結這么多,慢慢消化。