express服务 //解决跨域问题,浏览8080&服务3070 let express = require('express'); let app = express(); let port = 3070; //创建服务 app.listen(port,()=>{ console.log(`服务创建成功,正在监听${port}端口!`); }); 。。。 app.use((req,res,next)=>{ //可写res.header也可写res.setHeader res.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");//跨域不能写*通配符,8080和config/index.js里的port匹配的 res.setHeader("Access-Control-Allow-Credentials", true);//访问控制允许凭据,必须有 res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.setHeader("X-Powered-By",' 3.2.1')//可有可没有 if(req.method == "OPTIONS") { //让options请求快速返回,res.send改成res.sendStatus res.sendStatus(200); return; }; next(); }); 。。。
http服务 let http = require("http");//创建服务 。。。 //req请求,res响应 http.createServer((req,res)=>{//创建一个服务 //解决跨域问题,浏览8080&服务3070 res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.setHeader("X-Powered-By",' 3.2.1') if(req.method=="OPTIONS") return res.end();/*让options请求快速返回*/ 。。。 }).listen(3070);//监听一个端口
jq获取xml文件,跨域需后端设置
$.ajax({ type: "GET", url: "http://ddebugv3.xxxx.com/epc_svg/C3D_1S10411720116_OK_CN.interactivity.xml", dataType: "xml", success: (msg) => { console.log(1,msg) }, error: function (msg) { console.log(2,msg); } });
标签:node,Control,跨域,后端,res,Access,Allow,setHeader From: https://www.cnblogs.com/liufeiran/p/17276518.html