瀏覽器只對網絡請求有同源限制,同源就是協議、域名和端口號一致,不一樣源的客戶端腳本在沒有明確受權的狀況下,不能讀寫對方XHR資源,反之不一樣源腳本讀取對方XHR資源就是跨域。以http://www.a.com/test/index.html 的同源檢測舉例:html
//建立script發送請求
//請求返回執行cb函數,而且刪除建立的script
//相似於$ajax中的jsonp
function jsonp(url,params,cb){
return new Promimse((resolve,reject)=>{
window[cb] = function(data){
resolve(data);
document.body.removeChild(script);
}
params={...params,cb},
let arrs=[];
for(let key in params){
arrs.push(`${key}=${params[key]}`)
}
let script = document.createElement('script');
script.src= url + '?'+ arrs.join('&');
document.body.appendChild(script);
})
}
jsonp({
url:'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
params:{wd:%E8%B7%A8%E5%9F%9F},
cb:'show'}).then(data=>{
console.log(data)
})
複製代碼
配合iframes使用,假設a.html位於服務localhost:3000,b.html位於服務器localhost:4000前端
//a.html
<body>
<iframe id="frame" src="http://localhost:4000/b.html" frameborder="0" onload="load()"></iframe>
<script>
function load(){
let frame = document.getElementById('frame');
frame.contentWindow.postMessage('我很帥','http://localhost:4000');
window.onmessage =function (e){
console.log(e.data);
}
}
</script>
</body>
//otherWindow.postMessage(message, targetOrigin);
//otherWindow:指目標窗口,也就是給哪一個window發消息,是 window.frames 屬性的成員或者由 window.open 方法建立的窗口
//message:是要發送的消息,類型爲 String、Object (IE八、9 不支持)
//targetOrigin: 是限定消息接收範圍,不限制請使用'*'
//注意otherWindow和targetOrigin的區別
複製代碼
//b.html
<body>
<script>
//data:消息
//origin:消息來源地址
//source:源DOMWindow 對象
window.onmessage =function (e){
console.log(e.data);
e.source.postMessage('不要臉',e.origin);
}
</script>
</body>
複製代碼
//a.html
<body>
helloa
<iframe id="frame" src="http://www.kongbz.com/b.html" frameborder="0" onload="load()"></iframe>
<script>
document.domain = 'kongbz.com';//設置domain
function load(){
let frame = document.getElementById('frame');
console.log(frame.contentWindow.a)
}
</script>
</body>
複製代碼
<body>
hellob
<script>
document.domain = 'kongbz.com';//設置domain
var a = 'isB'
</script>
</body>
複製代碼
客戶端發送信息給服務端,若是想實現客戶端向客戶端通訊,只能經過頁面->服務端->另外一個頁面nginx
//客戶端
<body>
hellob
<script>
let socket = new WebSocket('ws://localhost:3000');
socket.onopen = function(){
socket.send('我很帥')
}
socket.onmessage = function(e){
console.log(e.data)
}
</script>
</body>
複製代碼
//服務端
let express = require('express');
let Websocket = require('wss');
let wss= new WebSocket.Server({port:3000})
wss.on('connection',function(ws){
ws.on('message',function(data){
console.log(data);
ws.send('不要臉');
})
})
let app = new express();
app.listen(3000)
複製代碼
const http = require('http')
const whitList = ['http://localhost:4000'];
http.createServer(function (req, res) {
let origin = req.headers.origin;
//在白名單中的域名才能訪問
if(whitList.includes(origin)){
//容許的域名(* 全部域),*不能和Access-Control-Allow-Credentials一塊兒使用
res.header("Access-Control-Allow-Origin", "*");
//容許攜帶哪一個頭訪問,不設置不能攜帶參數
res.header("Access-Control-Allow-Headers","ContentType");
//容許的方法,不設置默認支持GET、HEAD、POST,其餘類型必須設置才能處理請求
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//運行攜帶cookie,設置以後還能服務器才能接受cookie
res.header("Access-Control-Allow-Credentials",true);
//容許前端獲取哪一個頭,不設置瀏覽器不能解析後臺返回的參數
res.header("Access-Control-Allow-Expose-Headers",'ContentType');
if(req.method=== 'OPTIONS'){
res.end()
}
}
}).listen(9000, function () {
console.log('server is runing at 9000')
})
複製代碼
例如test.a.cn/index.html頁面去調用test.b.cn/service.jsongit
//nginx.conf
location / {
root;
index index.html index.htm;
}
location ~.*\.json {
root json;
add_header "Access-Control-Allow-Origin" "*";
}
複製代碼
IT即互聯網技術,從事的工做和網絡有很大的關係,前端要負責和後臺(服務器)進行交互,其必然得通過網絡,因此懂點網絡知識有很大的幫助。接下來會介紹:github