前后端分离部署存在跨域问题 ,每次前端请求后端都会发送一个option请求。这里可以通过配置代理来去掉跨域问题。
项目是通过 vue-cli生成的 , 所以有config文件夹 下面有 index.js :
找到 以下代码:
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
proxyTable里面添加以下内容:
proxyTable: {
'/api': {
target: 'http://localhost:9090/',//后端的访问地址
secure: false, //https请换成true,
changeOrigin: true, //是否跨域 跨域选择true
pathRewrite:{'^/api':'/api'}
}
},
这里 假设我们后端项目启动的地址以及端口是:http://localhost:9090.那么我们前端访问后端的请求 (axios来处理 ):
//获取当前用户
axios.post("/api/user/getNowUser",data).then(response => response.data);
假设前端启动端口是 8080,那么上面访问的代码是 http://localhost:8080/api/user/getNowUser 这