本文的目的是为了验证对跨域的理解——前端需要做相应的配置。posetMessage形式也可用。====项目地址(https://github.com/L6zt/corssDomain-demo)原理说明:什么是jsonp叫jsonp请求,其实相当于在页面加载远程脚本资源,页面会在加载函数后执行,这就是为什么你会发现jsonp1({a:1});jsonp1是jsonp请求结果中的一个函数(就像jquery已经把这个函数放到window下了),jsonp1({a:1})是相当执行函数的,所以可以拿到数据参数。cors是xhr2提供的跨域方法,cors文件是我的后端节点服务器。我是按照这个文档写的,纯手工制作。`constKoa=require('koa');//路由constRouter=require('koa-router');//constpath=require('path');//提供静态文件服务constkoaStaticServer=require('koa-static-server');//打开浏览器constopn=require('better-opn');//服务器1constwwwLocalhostComServer=newKoa();//服务器2constmLocalhostComServer=newKoa();//跨域路由constwwwCorsRouter=newRouter({prefix:'/cors'});//非跨域路由constwwwNotCorsRouter=newRouter({prefix:'/nm'});//jsonp请求constwwwJsonpRouter=newRouter({prefix:'/jsonp'});//跨域接口wwwCorsRouter.use('/',(ctx,next)=>{const{request}=ctx;const{header:{origin},method}=request;ctx.response.set('Access-Control-Allow-Origin',origin);next();}).post('/setCookie',(ctx,next)=>{constresult={flag:1,data:{message:'wesetcookieindifferentdomain',}};ctx.response.set('Access-Control-Allow-Credentials','true');ctx.response.set('Set-Cookie',auth=love;Expries=${newDate(Date.now()+60*60*24*10*1000)};路径=/);ctx.cookies.set('inject','love',{expires:newDate(Date.now()+60*60*24*10*1000),});ctx.response.set('内容类型','application/json');ctx.body=JSON.stringify(result);}).post('/normalData',(ctx,next)=>{constresult={flag:1,data:{message:'我们喜欢彼此'}}ctx.response.set('Content-type','application/json');ctx.body=JSON.stringify(result);});//非跨域接口wwwNotCorsRouter.use('/',(ctx,next)=>{ctx.response.set('Custom-Server-Sp','love');next();}).post('/data',(ctx,next)=>{constresult={flag:1,data:{message:'我们不在同一个域',}};ctx.response.set('Content-type','application/json');ctx.body=JSON.stringify(result);});//jsonp接口wwwJsonpRouter.get('/data',(ctx,next)=>{const{request}=ctx;const{header:{origin},method,query}=request;const{callback}=query;if(!callback){ctx.body=JSON.stringify({flag:0,message:'参数验证失败',})return}else{//相当于返回js文件//文本内容js文件ctx.response.set('Content-type','application/javascript');ctx.body=[`${callback}(`,JSON.stringify({flag:0,data:{payload:{key:'love'}}}),')'].join('');}})wwwLocalhostComServer。use((ctx,next)=>{const{request}=ctx;const{header:{origin,path},method}=request;//处理一些特殊的cors细节mdn文档if(method==='OPTIONS'){ctx.response.set('Access-Control-Allow-Origin',origin);ctx.response.set('Access-Control-Request-Method','POST');ctx.response.set('Access-Control-Allow-Headers','Content-Type');ctx.response.set('Access-Control-Allow-Credentials',true);ctx.body=''}else{next();}});wwwLocalhostComServer.use(wwwCorsRouter.routes());wwwLocalhostComServer.use(wwwNotCorsRouter.routes());wwwLocalhostComServer.use(wwwJsonpRouter.routes());mLocalhostComServer.use(koaStaticServer({rootDir:path.join(__dirname,'./m.assert')}));wwwLocalhostComServer.use(koaStaticServer({rootDir:path.join(__dirname,'./www.assert'),}));wwwLocalhostComServer.listen(7001,()=>{console.log('--*');控制台。log('7001端口接口启动');console.log('---');});mLocalhostComServer.listen(7002,()=>{console.log('--*');console.log('7002端口的服务器启动');console.log('---');opn('http://localhost:7002');});`
