題目1: 什么是同源策略
題目2: 什么是跨域?跨域有幾種實現(xiàn)形式
- 跨域就是在不同本域下的接口進行交互。
- 跨域的實現(xiàn)形式有:
1.JSONP。
2.CORS。
3.降域。
4.postMessage。
題目3: JSONP 的原理是什么
- JSONP的原理是利用例如<script>中的"src"這個屬性,這個屬性可以不受瀏覽器的同源策略限制進行跨域,不過需要后端支持。
- 具體步驟:
1.定義數(shù)據(jù)處理函數(shù)_fun
2.創(chuàng)建script標簽,src的地址執(zhí)行后端接口,最后加個參數(shù)callback=_fun
3.服務(wù)端在收到請求后,解析參數(shù),計算返還數(shù)據(jù),輸出 fun(data) 字符串。
4..fun(data)會放到script標簽做為js執(zhí)行。此時會調(diào)用fun函數(shù),將data做為參數(shù)。
題目4: CORS是什么
- CORS 全稱是跨域資源共享(Cross-Origin Resource Sharing),是一種 ajax 跨域請求資源的方式,支持現(xiàn)代瀏覽器,IE支持10以上。 實現(xiàn)方式很簡單,當(dāng)你使用 XMLHttpRequest 發(fā)送請求時,瀏覽器發(fā)現(xiàn)該請求不符合同源策略,會給該請求加一個請求頭:Origin,后臺進行一系列處理,如果確定接受請求則在返回結(jié)果中加入一個響應(yīng)頭:Access-Control-Allow-Origin; 瀏覽器判斷該相應(yīng)頭中是否包含 Origin 的值,如果有則瀏覽器會處理響應(yīng),我們就可以拿到響應(yīng)數(shù)據(jù),如果不包含瀏覽器直接駁回,這時我們無法拿到響應(yīng)數(shù)據(jù)。所以 CORS 的表象是讓你覺得它與同源的 ajax 請求沒啥區(qū)別,代碼完全一樣。
題目5: 根據(jù)視頻里的講解演示三種以上跨域的解決方式 ,寫成博客
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>news</title>
<style>
.container{
width: 900px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<ul class="news">
<li>第11日前瞻:中國沖擊4金 博爾特再戰(zhàn)</li>
<li>男雙力爭會師決賽 </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';//創(chuàng)建一個<script>標簽,并且設(shè)置它的src屬性為需要請求的地址,設(shè)置返回的包裹函數(shù)名字為appendHtml.
document.head.appendChild(script);
document.head.removeChild(script);//在head中添加這個標簽,運行之后再刪除,以免重復(fù)點擊增加代碼行數(shù)
})
function appendHtml(news){
var html = '';
for( var i=0; i<news.length; i++){
html += '<li>' + news[i] + '</li>';
}//在數(shù)據(jù)到來之前先設(shè)置好函數(shù)的內(nèi)容,讓數(shù)據(jù)到來之后便直接運行。
console.log(html);
$('.news').innerHTML = html;
}
function $(id){
return document.querySelector(id);
}
</script>
</html>
app.get('/getNews', function(req, res){
var news = [
"第11日前瞻:中國沖擊4金 博爾特再戰(zhàn)200米羽球",
"正直播柴飚/洪煒出戰(zhàn) 男雙力爭會師決賽",
"女排將死磕巴西!郎平安排男陪練模仿對方核心",
"沒有中國選手和巨星的110米欄 我們還看嗎?",
"中英上演奧運金牌大戰(zhàn)",
"博彩賠率挺中國奪回第二紐約時報:中國因?qū)κ址幎鴣G失的獎牌最多",
"最“出柜”奧運?同性之愛閃耀里約",
"下跪拜謝與洪荒之力一樣 都是真情流露"
]
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;//在請求到來的時候確認是否需要包裹函數(shù),也就是后端需要配合的地方。
if(cb){
res.send(cb + '('+ JSON.stringify(data) + ')');
}else{
res.send(data);
}
})
- JSONP主要思路就是利用<script>標簽的src屬性不受同源策略的限制來進行跨域??梢岳斫鉃榇舐纷卟煌ǎ硗庾咭粭l小路來到達目的地??梢灾苯訉SONP封裝成一個函數(shù),以便反復(fù)調(diào)用,與AJAX使用方法類似,但是原理完全不同。
function jsonp(url,data,callback){
var script = document.createElement('script');//創(chuàng)建一個script標簽
url = url + "?callback=" + callback;//拼接回調(diào)函數(shù)名字
if(data){
for(key in data){
url = url + "&" + key + "=" + data[key];//如果有需要的參數(shù)輸入,將參數(shù)也拼接進url
}
}
script.src = url;
document.head.appendChild(script);//創(chuàng)建script標簽發(fā)送請求。
document.head.removeChild(script);//完成之后刪除窗前的script標簽。
}
function callback(){
data[1] = ...........
}
- CORS前端寫法,與ajax沒有區(qū)別,區(qū)別主要在于后端
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>news</title>
<style>
.container{
width: 900px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<ul class="news">
<li>第11日前瞻:中國沖擊4金 博爾特再戰(zhàn)</li>
<li>男雙力爭會師決賽 </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>
- CORS后端寫法,需要在返回數(shù)據(jù)的響應(yīng)頭前加上"Access-Control-Allow-Origin",并且寫上允許的地址,如果寫"*",則表示允許所有地址從這里獲得數(shù)據(jù)??梢岳斫鉃榇舐繁徽系K封鎖,疏通障礙之后再走。缺點是只有ie10以上的瀏覽器才會兼容。
app.get('/getNews', function(req, res){
var news = [
"第11日前瞻:中國沖擊4金 博爾特再戰(zhàn)200米羽球",
"正直播柴飚/洪煒出戰(zhàn) 男雙力爭會師決賽",
"女排將死磕巴西!郎平安排男陪練模仿對方核心",
"沒有中國選手和巨星的110米欄 我們還看嗎?",
"中英上演奧運金牌大戰(zhàn)",
"博彩賠率挺中國奪回第二紐約時報:中國因?qū)κ址幎鴣G失的獎牌最多",
"最“出柜”奧運?同性之愛閃耀里約",
"下跪拜謝與洪荒之力一樣 都是真情流露"
]
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);
})
- window.postMessage() 方法提供了一種受控機制來規(guī)避同源策略限制,只要正確的使用,這種方法就很安全。它可以讓一個特定的地址(第二個參數(shù),如果寫"*"則表示所有地址都允許)來獲取某個參數(shù)(第一個參數(shù))。
- postMessage——a.html
<html>
<style>
.ct{
width: 910px;
margin: auto;
}
.main{
float: left;
width: 450px;
height: 300px;
border: 1px solid #ccc;
}
.main input{
margin: 20px;
width: 200px;
}
.iframe{
float: right;
}
iframe{
width: 450px;
height: 300px;
border: 1px dashed #ccc;
}
</style>
<div class="ct">
<h1>使用postMessage實現(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>
<html>
<style>
html,body{
margin: 0;
}
input{
margin: 20px;
width: 200px;
}
</style>
<input id="input" type="text" placeholder="http://b.jrg.com:8080/b.html">
<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>
</html>