前端跨域問題javascript
一:同源策略
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'是報錯的
二:跨域方案html
共有幾種解決方案:前端
(1)document.domain + iframehtml5
(2)動態建立scriptjava
(3)window.name + iframenginx
(4)window.postMessagejson
(5)CORSapi
(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) { var head = document.head || document.getElementByTagName('head')[0]; var script = 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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> 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 } </script> </head> <body> <iframe id="proxy" src="b.com/b.html" onload="getData()"></iframe> </body> </html>
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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script> var iframe = document.getElementById('iframe'); iframe.contentWindow.postMessage('我是a.com/index.hmtl的消息', '*'); </script> </head> <body> <iframe id="iframe" src="b.com/index.html"></iframe> </body> </html>
b.com/index.html
<script> window.onmessage = function(e){ e = e || event; alert(e.data) } </script>
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 }
好了,就總結這麼多,慢慢消化。