題目1: 什么是同源策略
瀏覽器出于安全方面的考慮厂捞,只允許與本域下的接口交互耸采。不同源的客戶端腳本在沒(méi)有明確授權(quán)的情況下福也,不能讀寫對(duì)方的資源。
本域指的是孽椰?
同協(xié)議:如都是http,https,file,ssh,mailto,tel
同域名(在//后到第一個(gè)/之間):
如都是http://jirengu.com/a 和http://jirengu.com/b
同端口:如都是80端口
如:http://jirengu.com/a/b.js 和 http://jirengu.com/index.php (同源)不同源的例子:
http://jirengu.com/main.js 和 https://jirengu.com/a.php (協(xié)議不同)
http://jirengu.com/main.js 和 http://bbs.jirengu.com/a.php (域名不同昭娩,域名必須完全相同才可以)
http://jiengu.com/main.js 和 http://jirengu.com:8080/a.php (端口不同,第一個(gè)是80)需要注意的是: 對(duì)于當(dāng)前頁(yè)面來(lái)說(shuō)頁(yè)面存放的 JS 文件的域不重要凛篙,重要的是加載該 JS 頁(yè)面所在什么域與JS中加載數(shù)據(jù)的域是否相同
題目2: 什么是跨域?跨域有幾種實(shí)現(xiàn)形式
什么是跨域栏渺?
允許不同域的接口進(jìn)行交互跨域有幾種實(shí)現(xiàn)形式
- JSONP(實(shí)際就是以加載js的方式,執(zhí)行拿到不同域名中的數(shù)據(jù))
- CORS(后端設(shè)置后端會(huì)添加一個(gè)響應(yīng)頭:Access-Control-Allow-Origin.設(shè)置允許訪問(wèn)的域名)
- 降域(幾個(gè)域名相同的一部分)
- postMessage(用于iframe,在parent的html中獲取iframe,發(fā)消息,在iframe的html中監(jiān)聽)
題目3: JSONP 的原理是什么
后端將數(shù)據(jù)轉(zhuǎn)換成js代碼的格式,前端通過(guò)script標(biāo)簽接收來(lái)自后端的js代碼再與前端已有的js代碼一起運(yùn)行實(shí)現(xiàn)跨域獲取數(shù)據(jù)
題目4: CORS是什么
前端用 XMLHttpRequest 跨域訪問(wèn)時(shí)呛梆,瀏覽器會(huì)在請(qǐng)求頭中添加:origin.后端會(huì)添加一個(gè)響應(yīng)頭:Access-Control-Allow-Origin瀏覽器判斷該相應(yīng)頭中Access-Control-Allow-Origin的值是否包含 Origin 的值,如果有則瀏覽器會(huì)處理響應(yīng)磕诊,我們就可以拿到響應(yīng)數(shù)據(jù)削彬,如果不包含瀏覽器直接駁回,這時(shí)我們無(wú)法拿到響應(yīng)數(shù)據(jù)秀仲。
題目5: 根據(jù)視頻里的講解演示三種以上跨域的解決方式
JSONP跨域
- a.html代碼
<script>
var scri = document.createElement('script')
scri.src = 'http://gaoyang2:8080/alert?callBack=myalert'
document.head.appendChild(scri)
document.head.removeChild(scri)
function myalert(str){
alert(str)
}
</script>
- router.js代碼
app.get('/alert', function(req, res) {
var call = req.query.callBack
var data = 'JSONP-跨域';
res.send(call+'('+JSON.stringify(data)+')');
});
CORS跨域
- a.html代碼
<script>
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function(){
if (xhr.readyState === 4) {
if(xhr.status == 200 || xhr.status == 304){
alert(JSON.parse(xhr.responseText))
}
}
}
xhr.open('get','http://gaoyang2:8080/alert',true)
xhr.send();
</script>
- router.js代碼
app.get('/alert', function(req, res) {
var data = 'CORS-跨域';
res.header("Access-Control-Allow-Origin", "http://gaoyang1:8080");
res.send(JSON.stringify(data));
});
降域(1.首先必須是有相同的一部分,2.相同的一部分必須域名的最后一部分3.降域的域名不能只有一個(gè)頂級(jí)域名)
- a.html代碼
<body>
<input type="text" placeholder="a.html">
<iframe src="http://2.gaoyang.com:8080/b.html" frameborder="0"></iframe>
<script>
document.querySelector('input').addEventListener('input', function(){
window.frames[0].document.querySelector('input').value = this.value;
})
document.domain = 'gaoyang.com'
</script>
</body>
- b.html代碼
<body>
<input type="text" placeholder="b.html">
<script>
document.querySelector('input').addEventListener('input', function(){
window.parent.document.querySelector('input').value = this.value;
})
document.domain = 'gaoyang.com'
</script>
</body>
postmessage
- a.html代碼
<body>
<input type="text" placeholder="a.html">
<iframe src="http://2.gaoyang.com:8080/b.html" frameborder="0"></iframe>
<script>
document.querySelector('input').addEventListener('input', function(){
window.frames[0].postMessage(this.value,'*')
})
window.addEventListener('message',function(e) {
document.querySelector('input').value = e.data
});
</script>
</body>
- b.html代碼
<body>
<input type="text" placeholder="b.html">
<script>
document.querySelector('input').addEventListener('input', function(){
window.parent.postMessage(this.value,'*')
})
window.addEventListener('message',function(e) {
document.querySelector('input').value = e.data
});
</script>
</body>