一,常用的jsonp跨域
1,在瀏覽器里面能拼得出來(lái),但是正常代碼請(qǐng)求返回如下:故代碼不同源,需要進(jìn)行跨域請(qǐng)求

Paste_Image.png
1,jsonp 只能get請(qǐng)求
2,document.domain +iframe 主域相同子域不同
3,windows.name +iframe 不同頁(yè)面不同域名都可以,且name可以支持非常長(zhǎng)的String值
4,cors 服務(wù)器端處理
5,location.hash +iframe 不分,利用代理
6,HTML postMessage 支持度挺高的
跨域請(qǐng)求方式如下:
1,jsonp請(qǐng)求
注意:jsonp是利用src不受同源策略影響來(lái)實(shí)現(xiàn)的,src只能get請(qǐng)求方式
不能解決不同域的兩個(gè)頁(yè)面之間的如何進(jìn)行JavaScript調(diào)用的問(wèn)題
使用ajax調(diào)用jsonp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSONP實(shí)現(xiàn)跨域2</title>
</head>
<body>
<div id="mydiv">
<button id="btn">點(diǎn)擊</button>
</div>
</body>
<script type="text/javascript">
function handleResponse(response){
console.log(response);
}
</script>
<script type="text/javascript">
window.onload = function() {
var oBtn = document.getElementById('btn');
oBtn.onclick = function() {
var script = document.createElement("script");
script.src = "https://api.douban.com/v2/book/search?q=javascript&count=1&callback=handleResponse";
document.body.insertBefore(script, document.body.firstChild);
使用jquery的$ajax()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery實(shí)現(xiàn)JSONP</title>
</head>
<body>
<div id="mydiv">
<button id="btn">點(diǎn)擊</button>
</div>
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.ajax({
async : true,
url : "https://api.douban.com/v2/book/search",
type : "GET",
dataType : "jsonp", // 返回的數(shù)據(jù)類(lèi)型,設(shè)置為JSONP方式
jsonp : 'callback', //指定一個(gè)查詢(xún)參數(shù)名稱(chēng)來(lái)覆蓋默認(rèn)的 jsonp 回調(diào)參數(shù)名 callback
jsonpCallback: 'handleResponse', //設(shè)置回調(diào)函數(shù)名
data : {
q : "javascript",
count : 1
},
success: function(response, status, xhr){
console.log('狀態(tài)為:' + status + ',狀態(tài)是:' + xhr.statusText);
console.log(response);
}
});
});
});
</script>
</html>
通過(guò)getJSON()來(lái)實(shí)現(xiàn)jsonp跨域
$.getJSON("https://api.douban.com/v2/book/search?q=javascript&count=1&callback=?", function(data){
console.log(data);
});
2,主域相同,子域不同,document.domain + iframe
注意:形式可以是一個(gè)頁(yè)面里面嵌套iframe(另一個(gè)頁(yè)面或者域之間的數(shù)據(jù)通信)
實(shí)現(xiàn)方式如下:可以把2個(gè)界面的document.domain設(shè)置成一樣的,但是只能設(shè)置成本身,后者更高一級(jí)的父域才行。
在頁(yè)面(http://www.damonare.cn/a.html) 中設(shè)置document.domain:
<iframe id = "iframe" src="http://damonare.cn/b.html" onload = "test()"></iframe>
<script type="text/javascript">
document.domain = 'damonare.cn';//設(shè)置成主域
function test(){
alert(document.getElementById('iframe').contentWindow);//contentWindow 可取得子窗口的 window 對(duì)象
}
</script>
------------------------------------------------------------------
在頁(yè)面(http://damonare.cn/b.html) 中也設(shè)置document.domain:
<script type="text/javascript">
document.domain = 'damonare.cn';//在iframe載入這個(gè)頁(yè)面也設(shè)置document.domain,使之與主頁(yè)面的document.domain相同
</script>
3,通過(guò)location.hash + + iframe跨域
主域不同
4,通過(guò)HTML5的postMessage方法跨域
兩個(gè)頁(yè)面之間的通信
A頁(yè)面通過(guò)postMessage方法發(fā)送消息:
window.onload = function() {
var ifr = document.getElementById('ifr');
var targetOrigin = "http://www.google.com";
ifr.contentWindow.postMessage('hello world!', targetOrigin);
};
B頁(yè)面通過(guò)message事件監(jiān)聽(tīng)并接受消息:
var onmessage = function (event) {
var data = event.data;//消息
var origin = event.origin;//消息來(lái)源地址
var source = event.source;//源Window對(duì)象
if(origin=="http://www.baidu.com"){
console.log(data);//hello world!
}
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', onmessage, false);
} else if (typeof window.attachEvent != 'undefined') {
//for ie
window.attachEvent('onmessage', onmessage);
}
5,使用 CORS跨域
瀏覽器和服務(wù)器之間的通信,思想就是使用自定義HTTP頭部讓瀏覽器和服務(wù)器進(jìn)行通信
平時(shí)正常寫(xiě)代碼
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://segmentfault.com/u/trigkit4/",true);
xhr.send();
</script>
服務(wù)器設(shè)置請(qǐng)求頭:Access-Control-Allow-Origin啟用CORS。
Access-Control-Allow-Origin:*//或者是通配符
兼容:IE10+
6,使用windows.name + + iframe跨域
** windows都有一個(gè)name屬性,在頁(yè)面載入期間,都是共享應(yīng)name屬性**
比如:我們?cè)谌我庖粋€(gè)頁(yè)面輸入
window.name = "My window's name";
setTimeout(function(){
window.location.;
},1000)
--------------------------
進(jìn)入damonare.cn頁(yè)面后我們?cè)贆z測(cè)再檢測(cè) window.name :
window.name; // My window's name