解决vue请求跨域问题
vue-element-admin关掉mock改为本地后端项目api后报错:Network Error
Request URL: http://localhost:8082/api/admin/login
Referrer Policy: strict-origin-when-cross-origin
Request URL: http://localhost:8082/api/admin/login
Request Method: OPTIONS
Status Code: 403
Remote Address: [::1]:8082
Referrer Policy: strict-origin-when-cross-origin
解决方案
在vue.config.js
文件配置代理
所有访问/api
的都代理target
的URL
//vue.config.js
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
'/api': {
target: 'http://localhost:8082',
changeOrigin: true
}
}
},
我的环境配置:
//.env.development
# just a flag
ENV = 'development'
# base api
VUE_APP_BASE_API = '/api/admin'
前后对比