1、在vue里面使用http代理,在vue项目里根目录下找到
vue.config.js文件,在
proxy中设置跨域
devServer: {
proxy: { //配置跨域
'/api': {
target: 'http://192.0.0.1/', //这里后台的地址模拟的;应该填写你们真实的后台接口
changOrigin: true, //允许跨域
pathRewrite: {
/* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
*/
'^/api': ''
}
},
}
},
1、后台node.js引入Cros模块,
const cors = require('cors');
app.use(cors({
origin:[' XXXX' ] ,
//这里面的元素就是地址,意思为多个地址之间互相访问不会被拦截,相当于白名单,可以添加四五个
}));