浏览器只对网络请求有同源限制,同源就是协议、域名和端口号一致,不一样源的客户端脚本在没有明确受权的状况下,不能读写对方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