1.cors跨域
app.all("*",function(req,res,next){ //设置允许跨域的域名,*代表允许任意域名跨域 res.header("Access-Control-Allow-Origin","*"); //允许的header类型 res.header("Access-Control-Allow-Headers","content-type"); //跨域允许的请求方式 res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS"); if (req.method.toLowerCase() == 'options') res.send(200); //让options尝试请求快速结束 else next(); }
2.jsonp跨域
动态创建script标签,利用src属性可以跨域的特性 请求的url拼接回调函数
3.vue代理
vue2.0
在config下面的index.js中
proxyTable: {
'/api': {
target: 'http://192.168.40.202:9092/api',//跨域的地址
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
},
},
vue3.0
在根目录下自己创建vue.config.js
添加如下代码
module.exports = {
devServer:{
host: '127.0.0.1',
proxy:{
'/api':{
// 代理请求时的localhost的值为target
target:"http://m.jxsjs.com/api/",
changeOrigin:true,
pathRewrite: {
'/api': ''
}
}
}
}
}
最后记得重启项目哦