題目1: 什么是同源策略
瀏覽器出于安全方面的考慮,只允許與本域下的接口交互。不同源的客戶端腳本在沒有明確授權的情況下,不能讀寫對方的資源。
本域指的是?
同協議:如都是http,https,file,ssh,mailto,tel
同域名(在//后到第一個/之間):
如都是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 (協議不同)
http://jirengu.com/main.js 和 http://bbs.jirengu.com/a.php (域名不同,域名必須完全相同才可以)
http://jiengu.com/main.js 和 http://jirengu.com:8080/a.php (端口不同,第一個是80)需要注意的是: 對于當前頁面來說頁面存放的 JS 文件的域不重要,重要的是加載該 JS 頁面所在什么域與JS中加載數據的域是否相同
題目2: 什么是跨域?跨域有幾種實現形式
什么是跨域?
允許不同域的接口進行交互跨域有幾種實現形式
- JSONP(實際就是以加載js的方式,執行拿到不同域名中的數據)
- CORS(后端設置后端會添加一個響應頭:Access-Control-Allow-Origin.設置允許訪問的域名)
- 降域(幾個域名相同的一部分)
- postMessage(用于iframe,在parent的html中獲取iframe,發消息,在iframe的html中監聽)
題目3: JSONP 的原理是什么
后端將數據轉換成js代碼的格式,前端通過script標簽接收來自后端的js代碼再與前端已有的js代碼一起運行實現跨域獲取數據
題目4: CORS是什么
前端用 XMLHttpRequest 跨域訪問時,瀏覽器會在請求頭中添加:origin.后端會添加一個響應頭:Access-Control-Allow-Origin瀏覽器判斷該相應頭中Access-Control-Allow-Origin的值是否包含 Origin 的值,如果有則瀏覽器會處理響應,我們就可以拿到響應數據,如果不包含瀏覽器直接駁回,這時我們無法拿到響應數據。
題目5: 根據視頻里的講解演示三種以上跨域的解決方式
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.降域的域名不能只有一個頂級域名)
- 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>