日韩AV女星一区二区三区_天堂无码v亚洲一本视_国产三级理论片_日韩欧美亚洲_细狗视频网页版可打开链接_色偷偷精品免费视频_久草国产在线_亚洲欧美成人aⅴ大片_国产女子私蜜保健养生馆_欧美在线综合高清

聰明屋視角

關(guān)注互聯(lián)網(wǎng),關(guān)注技術(shù)開發(fā),透析與分享移動互聯(lián)網(wǎng)行業(yè)最新動態(tài)

前端解決跨域問題的8種方案(最新最全)

時間:2019-02-18 18:19:01    閱讀:93639次 分類:原型UI交互
原文:http://www.cnblogs.com/jchen666/p/3399951.html 1.同源策略如下: url 說明 是否允許通信 http://www.a.com/a.js http://www.a.com/b.js 同一域名下 允許 http://www.a.com/lab/a.js http://www.a.com//b.js 同一域名下不同文件夾 允許 http://ww...

原文:http://www.cnblogs.com/JChen666/p/3399951.html


1.同源策略如下:

URL 說明 是否允許通信
http://www.a.com/a.js
http://www.a.com/b.js
同一域名下 允許
http://www.a.com/lab/a.js
http://www.a.com/script/b.js
同一域名下不同文件夾 允許
http://www.a.com:8000/a.js
http://www.a.com/b.js
同一域名,不同端口 不允許
http://www.a.com/a.js
https://www.a.com/b.js
同一域名,不同協(xié)議 不允許
http://www.a.com/a.js
http://70.32.92.74/b.js
域名和域名對應(yīng)ip 不允許
http://www.a.com/a.js
http://script.a.com/b.js
主域相同,子域不同 不允許
http://www.a.com/a.js
http://a.com/b.js
同一域名,不同二級域名(同上) 不允許(cookie這種情況下也不允許訪問)
http://www.cnblogs.com/a.js
http://www.a.com/b.js
不同域名 不允許
特別注意兩點:
第一,如果是協(xié)議和端口造成的跨域問題“前臺”是無能為力的,
第二:在跨域問題上,域僅僅是通過“URL的首部”來識別而不會去嘗試判斷相同的ip地址對應(yīng)著兩個域或兩個域是否在同一個ip上。
“URL的首部”指window.location.protocol +window.location.host,也可以理解為“Domains, protocols and ports must match”。

2. 前端解決跨域問題

1> document.domain + iframe      (只有在主域相同的時候才能使用該方法)

1) 在www.a.com/a.html中:

復(fù)制代碼
document.domain = 'a.com'; var ifr = document.createElement('iframe');
ifr.src = 'http://www.script.a.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;
};
復(fù)制代碼

2) 在www.script.a.com/b.html中:

document.domain = 'a.com';


2> 動態(tài)創(chuàng)建script

這個沒什么好說的,因為script標(biāo)簽不受同源策略的限制。

復(fù)制代碼
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, 0);
}
window.baidu = {
  sug: function(data){
    console.log(data);
  }
}
loadScript('http://suggestion.baidu.com/su?wd=w',function(){console.log('loaded')}); //我們請求的內(nèi)容在哪里? //我們可以在chorme調(diào)試面板的source中看到script引入的內(nèi)容
復(fù)制代碼


3> location.hash + iframe

原理是利用location.hash來進(jìn)行傳值。

假設(shè)域名a.com下的文件cs1.html要和cnblogs.com域名下的cs2.html傳遞信息。
1) cs1.html首先創(chuàng)建自動創(chuàng)建一個隱藏的iframe,iframe的src指向cnblogs.com域名下的cs2.html頁面
2) cs2.html響應(yīng)請求后再將通過修改cs1.html的hash值來傳遞數(shù)據(jù)

3) 同時在cs1.html上加一個定時器,隔一段時間來判斷l(xiāng)ocation.hash的值有沒有變化,一旦有變化則獲取獲取hash值
注:由于兩個頁面不在同一個域下IE、Chrome不允許修改parent.location.hash的值,所以要借助于a.com域名下的一個代理iframe

代碼如下:
先是a.com下的文件cs1.html文件:

復(fù)制代碼
function startRequest(){ var ifr = document.createElement('iframe');
    ifr.style.display = 'none';
    ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';
    document.body.appendChild(ifr);
} function checkHash() { try { var data = location.hash ? location.hash.substring(1) : ''; if (console.log) {
            console.log('Now the data is '+data);
        }
    } catch(e) {};
}
setInterval(checkHash, 2000);
復(fù)制代碼

cnblogs.com域名下的cs2.html:

復(fù)制代碼
//模擬一個簡單的參數(shù)處理操作 switch(location.hash){ case '#paramdo':
        callBack(); break; case '#paramset': //do something…… break;
} function callBack(){ try {
        parent.location.hash = 'somedata';
    } catch (e) { // ie、chrome的安全機(jī)制無法修改parent.location.hash, // 所以要利用一個中間的cnblogs域下的代理iframe var ifrproxy = document.createElement('iframe');
        ifrproxy.style.display = 'none';
        ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata'; // 注意該文件在"a.com"域下  document.body.appendChild(ifrproxy);
    }
}
復(fù)制代碼

a.com下的域名cs3.html

//因為parent.parent和自身屬于同一個域,所以可以改變其location.hash的值 parent.parent.location.hash = self.location.hash.substring(1);


4> window.name + iframe

window.name 的美妙之處:name 值在不同的頁面(甚至不同域名)加載后依舊存在,并且可以支持非常長的 name 值(2MB)。

1) 創(chuàng)建a.com/cs1.html

2) 創(chuàng)建a.com/proxy.html,并加入如下代碼

復(fù)制代碼




復(fù)制代碼

3 在b.com/cs1.html中包含:


 

5> postMessage(HTML5中的XMLHttpRequest Level 2中的API)

1) a.com/index.html中的代碼:

復(fù)制代碼
																															


復(fù)制代碼

2) b.com/index.html中的代碼:

復(fù)制代碼
 ,轉(zhuǎn)載請注明出處