題目1: 什么是同源策略
同源策略指協(xié)議牵素、域名鼎姊、端口都必須是是一致的周瞎,其中一種不一致即不同源黄琼。限制從一個(gè)源加載的文檔或腳本如何與來(lái)自另一個(gè)源的資源進(jìn)行交互樊销。這是一個(gè)用于隔離潛在惡意文件的關(guān)鍵的安全機(jī)制。
題目2: 什么是跨域脏款?跨域有幾種實(shí)現(xiàn)形式
跨域指允許不同域的接口進(jìn)行交互
JSONP :利用src屬性引入其他域下的js围苫,實(shí)現(xiàn)跨域訪問(wèn)接口,需要后端支持撤师。
CORS:全稱(chēng)是跨域資源共享( cross-origin-resource-shareing)IE10及以上支持
降域:只有同屬于一個(gè)域名的二級(jí)域名才能夠使用這種方式
postMessag:window.postMessage() 方法可以安全地實(shí)現(xiàn)跨源通信剂府。通常,當(dāng)且僅當(dāng)執(zhí)行它們的頁(yè)面位于具有相同的協(xié)議(通常為https)剃盾,端口號(hào)(443為https的默認(rèn)值)腺占,以及主機(jī)(模數(shù) Document.domain 由兩個(gè)頁(yè)面設(shè)置為相同的值)。 window.postMessage() 方法提供了一種受控機(jī)制痒谴,以便在正確使用時(shí)以安全的方式規(guī)避此限制衰伯。 window.postMessage() 方法被調(diào)用時(shí),會(huì)在所有頁(yè)面腳本執(zhí)行完畢之后(e.g., 在該方法之后設(shè)置的事件积蔚、之前設(shè)置的timeout 事件,etc.)向目標(biāo)窗口派發(fā)一個(gè) MessageEvent 消息意鲸。 該MessageEvent消息有四個(gè)屬性需要注意: message 屬性表示該message 的類(lèi)型; data 屬性為 window.postMessage 的第一個(gè)參數(shù)尽爆;origin 屬性表示調(diào)用window.postMessage() 方法時(shí)調(diào)用頁(yè)面的當(dāng)前狀態(tài)怎顾; source 屬性記錄調(diào)用 window.postMessage() 方法的窗口信息。
題目3: JSONP 的原理是什么
JSONP的原理是利用src屬性引入其他域下的JS漱贱,需要后端返回?cái)?shù)據(jù)是一個(gè)函數(shù)調(diào)用槐雾,處理后的數(shù)據(jù)作為函數(shù)的參數(shù)傳入。
- 定義數(shù)據(jù)處理函數(shù)_fun
- 創(chuàng)建script標(biāo)簽幅狮,src的地址執(zhí)行后端接口募强,最后加個(gè)參數(shù)callback=_fun
- 服務(wù)端在收到請(qǐng)求后,解析參數(shù)彪笼,計(jì)算返還數(shù)據(jù)钻注,輸出 fun(data) 字符串。
- fun(data)會(huì)放到script標(biāo)簽做為js執(zhí)行配猫。此時(shí)會(huì)調(diào)用fun函數(shù)幅恋,將data做為參數(shù)。
題目4: CORS是什么
CORS 全稱(chēng)是跨域資源共享(Cross-Origin Resource Sharing)泵肄,是一種 ajax 跨域請(qǐng)求資源的方式捆交,支持現(xiàn)代瀏覽器淑翼,IE支持10以上。 當(dāng)你使用 XMLHttpRequest 發(fā)送請(qǐng)求時(shí)品追,瀏覽器發(fā)現(xiàn)該請(qǐng)求不符合同源策略玄括,會(huì)給該請(qǐng)求加一個(gè)請(qǐng)求頭:Origin,后臺(tái)進(jìn)行一系列處理肉瓦,如果確定接受請(qǐng)求則在返回結(jié)果中加入一個(gè)響應(yīng)頭:Access-Control-Allow-Origin; 瀏覽器判斷該相應(yīng)頭中是否包含 Origin 的值遭京,如果有則瀏覽器會(huì)處理響應(yīng),我們就可以拿到響應(yīng)數(shù)據(jù)泞莉,如果不包含瀏覽器直接駁回哪雕,這時(shí)我們無(wú)法拿到響應(yīng)數(shù)據(jù)。所以 CORS 的表象是讓你覺(jué)得它與同源的 ajax 請(qǐng)求沒(méi)啥區(qū)別鲫趁,代碼完全一樣斯嚎。
**題目5: 根據(jù)視頻里的講解演示三種以上跨域的解決方式 **
JSONP:
前臺(tái):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>news</title>
</head>
<body>
<div class="container">
<ul class="news">
<li>第11日前瞻:中國(guó)沖擊4金 博爾特再戰(zhàn)</li>
<li>男雙力爭(zhēng)會(huì)師決賽 </li>
<li>女排將死磕巴西!</li>
</ul>
<button class="change">換一組</button>
</div>
<script>
$('.change').addEventListener('click', function(){
var script = document.createElement('script');
script.src = 'http://127.0.0.1/getNews?callback=appendHtml';
document.head.appendChild(script);
document.head.removeChild(script);
})
function appendHtml(news){
var html = '';
for( var i=0; i<news.length; i++){
html += '<li>' + news[i] + '</li>';
}
console.log(html);
$('.news').innerHTML = html;
}
function $(id){
return document.querySelector(id);
}
</script>
</html>
后臺(tái):
app.get('/getNews', function(req, res){
var news = [
"第11日前瞻:中國(guó)沖擊4金 博爾特再戰(zhàn)200米羽球",
"正直播柴飚/洪煒出戰(zhàn) 男雙力爭(zhēng)會(huì)師決賽",
"女排將死磕巴西挨厚!郎平安排男陪練模仿對(duì)方核心",
"沒(méi)有中國(guó)選手和巨星的110米欄 我們還看嗎堡僻?",
"中英上演奧運(yùn)金牌大戰(zhàn)",
"博彩賠率挺中國(guó)奪回第二紐約時(shí)報(bào):中國(guó)因?qū)κ址幎鴣G失的獎(jiǎng)牌最多",
"最“出柜”奧運(yùn)?同性之愛(ài)閃耀里約",
"下跪拜謝與洪荒之力一樣 都是真情流露"
]
var data = [];
for(var i=0; i<3; i++){
var index = parseInt(Math.random()*news.length);
data.push(news[index]);
news.splice(index, 1);
}
var cb = req.query.callback;
if(cb){
res.send(cb + '('+ JSON.stringify(data) + ')');
}else{
res.send(data);
}
})
CORS:
前臺(tái):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>news</title>
</head>
<body>
<div class="container">
<ul class="news">
<li>第11日前瞻:中國(guó)沖擊4金 博爾特再戰(zhàn)</li>
<li>男雙力爭(zhēng)會(huì)師決賽 </li>
<li>女排將死磕巴西疫剃!</li>
</ul>
<button class="change">換一組</button>
</div>
<script>
$('.change').addEventListener('click', function () {
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://b.jrg.com:8080/getNews', true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
appendHtml(JSON.parse(xhr.responseText))
}
}
window.xhr = xhr
})
function appendHtml(news) {
var html = '';
for (var i = 0; i < news.length; i++) {
html += '<li>' + news[i] + '</li>';
}
console.log(html);
$('.news').innerHTML = html;
}
function $(id) {
return document.querySelector(id);
}
</script>
</html>
后臺(tái):
app.get('/getNews', function(req, res){
var news = [
"第11日前瞻:中國(guó)沖擊4金 博爾特再戰(zhàn)200米羽球",
"正直播柴飚/洪煒出戰(zhàn) 男雙力爭(zhēng)會(huì)師決賽",
"女排將死磕巴西钉疫!郎平安排男陪練模仿對(duì)方核心",
"沒(méi)有中國(guó)選手和巨星的110米欄 我們還看嗎?",
"中英上演奧運(yùn)金牌大戰(zhàn)",
"博彩賠率挺中國(guó)奪回第二紐約時(shí)報(bào):中國(guó)因?qū)κ址幎鴣G失的獎(jiǎng)牌最多",
"最“出柜”奧運(yùn)慌申?同性之愛(ài)閃耀里約",
"下跪拜謝與洪荒之力一樣 都是真情流露"
]
var data = [];
for(var i=0; i<3; i++){
var index = parseInt(Math.random()*news.length);
data.push(news[index]);
news.splice(index, 1);
}
res.header("Access-Control-Allow-Origin", "http://a.jrg.com:8080");
//res.header("Access-Control-Allow-Origin", "*");
res.send(data);
})
降域:
通過(guò)修改document.domain來(lái)跨域操縱頁(yè)面上的iframne
可以設(shè)置一個(gè)隱藏的iframe頁(yè)面陌选,iframe頁(yè)面同源獲取信息后,設(shè)置降域蹄溉,將信息傳遞給主頁(yè)面
document.domain = "jrg.com"
<div class="ct">
<h1>使用降域?qū)崿F(xiàn)跨域</h1>
<div class="main">
<input type="text" placeholder="http://a.jrg.com:8080/a.html">
</div>
<iframe src="http://b.jrg.com:8080/b.html" frameborder="0" ></iframe>
</div>
postMassage:
a頁(yè)面:
<html>
<div class="ct">
<h1>使用postMessage實(shí)現(xiàn)跨域</h1>
<div class="main">
<input type="text" placeholder="http://a.jrg.com:8080/a.html">
</div>
<iframe src="http://localhost:8080/b.html" frameborder="0" ></iframe>
</div>
<script>
//URL: http://a.jrg.com:8080/a.html
$('.main input').addEventListener('input', function(){
console.log(this.value);
window.frames[0].postMessage(this.value,'*');
})
window.addEventListener('message',function(e) {
$('.main input').value = e.data
console.log(e.data);
});
function $(id){
return document.querySelector(id);
}
</script>
</html>
b頁(yè)面:
<script>
// URL: http://b.jrg.com:8080/b.html
$('#input').addEventListener('input', function(){
window.parent.postMessage(this.value, '*');
})
window.addEventListener('message',function(e) {
$('#input').value = e.data
console.log(e.data);
});
function $(id){
return document.querySelector(id);
}
</script>