解决跨域问题 最常用的两种方法
1、服务器端cors配置
在服务器端进行配置,在服务器端设置Access-Control-Allow-Origin即可
// 加载CORS模块
const cors = require('cors');
// 使用CORS中间件
server.use(cors({
origin: ['http://localhost:8080', 'http://127.0.0.1:8080', 'http://localhost:8081', 'http://127.0.0.1:8081']
}));//全部设置*
2、proxy代理跨域的方法
在项目根目录中创建vue.config.js文件修改配置
module.exports = {
devServer: { //再配置一个代理程序:
proxy: {
'/': { //代理内容从/开始
target: `http://127.0.0.1:8080`, //代理要跨域的地方 域名 在页面url获取直接写这个网址后面的内容
changeOrigin: true, //域名有变更
secure:false,//路由重写 转发规则
pathRewrite:{
//^代表正则表达式 xxx开头
“^/”:""
}
}
}
}
}
配置完记得重启