沒(méi)有自己購(gòu)買(mǎi)域名,服務(wù)器,如何練習(xí)跨域
在谷歌瀏覽器上模擬,設(shè)置兩個(gè)服務(wù)器的域名為http://127.0.0.1:2000和http://127.0.0.1:8080,8080向2000端口下的域請(qǐng)求數(shù)據(jù)
方法1:jsonp
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="container">
<div class="news">
<li>中二少時(shí)誦詩(shī)書(shū)</li>
<li>雙男力爭(zhēng)會(huì)師決賽</li>
<li>女排死磕巴西</li>
</div>
<button id="change">換一組</button>
</div>
<script type="text/javascript">
function $(x){
return document.querySelector(x)
}
$('#change').addEventListener('click',function(){
var script=document.createElement('script');
script.src='http://127.0.0.1:2000/page1.json?callback=appendHtml'
document.head.appendChild(script);
})
function appendHtml(data){
var html = '';
for(var i=0; i< data.length; i++){
html += '<li>'+data[i]+'</li>'
}
console.log(html)
$('.news').innerHTML = html;
}
</script>
</body>
</html>
router.js部分
app.get('/page1.json',function(req,res){
var news=[
'aaaaaaaa',
'bbbbbbbb',
'cccccccc',
'ddddddddd',
'eeeeeeeee',
'fffffffff',
'ggggggggg'
]
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;
console.log(cb)
if(cb){
res.send(cb+'('+JSON.stringify(data)+')');
}else{
res.send(data)
console.log(data)
}
//res.header("Access-Control-Allow-Origin",'*')
})
結(jié)果如圖
方法2:cors
修改一下代碼,改成向2000端口發(fā)送ajax請(qǐng)求
$('#change').addEventListener('click',function(){
/* var script=document.createElement('script');
script.src='http://127.0.0.1:2000/page1.json?callback=appendHtml'
document.head.appendChild(script);
*/
var xhr=new XMLHttpRequest;
xhr.onload=function(){
appendHtml(xhr.response)
}
xhr.open('GET','http://127.0.0.1:2000/page1.json',true)
xhr.send();
})
得到的結(jié)果如圖
現(xiàn)在我在2000端口的服務(wù)器上給8080端口的域授權(quán)
app.get('/page1.json',function(req,res){
var news=[
'啊啊啊啊啊啊a',
'反反復(fù)復(fù)付付付付',
'cの額鵝鵝鵝',
'd啛啛喳喳',
'少時(shí)誦詩(shī)書(shū)',
'f啦啦啦啦綠',
'世世代代發(fā)我'
]
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;
console.log(cb)
if(cb){
res.send(cb+'('+JSON.stringify(data)+')');
}else{
res.send(data)
console.log(data)
}*/
res.header("Access-Control-Allow-Origin","http://127.0.0.1:8080")
res.send(data)
})
得到的結(jié)果如圖
這樣就實(shí)現(xiàn)了跨域
方法3postMessege
window.postMessage()方法可以安全地實(shí)現(xiàn)跨源通信。通常,當(dāng)且僅當(dāng)執(zhí)行它們的頁(yè)面位于具有相同的協(xié)議(通常為https),端口號(hào)(443為https的默認(rèn)值),以及主機(jī)(模數(shù)?[Document.domain
](https://developer.mozilla.org...?由兩個(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
](https://developer.mozilla.org...?消息。 該[MessageEvent
](https://developer.mozilla.org...消息有四個(gè)屬性需要注意: message 屬性表示該message 的類型;?data 屬性為?window.postMessage?的第一個(gè)參數(shù);origin 屬性表示調(diào)用window.postMessage()方法時(shí)調(diào)用頁(yè)面的當(dāng)前狀態(tài);?source 屬性記錄調(diào)用?window.postMessage()方法的窗口信息。
postMessage的用法為:otherWindow.postMessage(message, targetOrigin, [transfer]);
第一個(gè)參數(shù)為:我要發(fā)送的data,
第二個(gè)參數(shù)為:通過(guò)窗口的origin屬性來(lái)指定哪些窗口能接收到消息事件,其值可以是字符串""(表示無(wú)限制)或者一個(gè)URI,在發(fā)送消息的時(shí)候,如果目標(biāo)窗口的協(xié)議、主機(jī)地址或端口這三者的任意一項(xiàng)不匹配targetOrigin提供的值,那么消息就不會(huì)被發(fā)送;只有三者完全匹配,消息才會(huì)被發(fā)送。這個(gè)機(jī)制用來(lái)控制消息可以發(fā)送到哪些窗口;例如,當(dāng)用postMessage傳送密碼時(shí),這個(gè)參數(shù)就顯得尤為重要,必須保證它的值與這條包含密碼的信息的預(yù)期接受者的orign屬性完全一致,來(lái)防止密碼被惡意的第三方截獲。如果你明確的知道消息應(yīng)該發(fā)送到哪個(gè)窗口,那么請(qǐng)始終提供一個(gè)有確切值的targetOrigin,而不是。不提供確切的目標(biāo)將導(dǎo)致數(shù)據(jù)泄露到任何對(duì)數(shù)據(jù)感興趣的惡意站點(diǎn)。
第三個(gè)參數(shù)為:Transfer,可選的參數(shù),是一串和message 同時(shí)傳遞的?[Transferable
](https://developer.mozilla.org...?對(duì)象. 這些對(duì)象的所有權(quán)將被轉(zhuǎn)移給消息的接收方,而發(fā)送一方將不再保有所有權(quán)。
如圖 在index設(shè)置兩個(gè)frame元素
index代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<iframe src="1.html" width="300px" height="300px"></iframe>
<iframe src="2.html" width="300px" height="300px"></iframe>
</body>
</html>
frames[0]代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input id="i1" value="" placeholder="1-input"/>
<script type="text/javascript">
function $(x){
return document.querySelector(x)
}
$('#i1').addEventListener('input',function(){
//console.log('this.input.value:'+this.value);
window.parent.frames[1].postMessage(this.value,'*');
// 給本頁(yè)面父節(jié)點(diǎn)的frame[1]發(fā)送消息
//console.log(window.frames[0].postMessage);
})
//接受發(fā)送的消息,因上面沒(méi)有指定給自己傳輸,所以收不到上面的消息
window.addEventListener('message',function(e){
$('#i1').value=e.data;
//console.log('e.data:'+e.data)
})
</script>
</body>
</html>
frame[1]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input id="i2" value="" placeholder="2-input"/>
<script type="text/javascript">
function $(x){
return document.querySelector(x)
}
$('#i2').addEventListener('input',function(){
//console.log('this.input.value:'+this.value);
window.parent.frames[0].postMessage(this.value,'*')
// 給本頁(yè)面父節(jié)點(diǎn)的frame[1]發(fā)送消息
})
//接受發(fā)送的消息,因上面沒(méi)有指定給自己傳輸,所以收不到上面的消息
window.addEventListener('message',function(e){
$('#i2').value=e.data;
})
</script>
</body>
</html>
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。