Access to XMLHttpRequest at‘http://localhost:跨域問題解決
阿新 • • 發佈:2021-02-11
在vue.config.js裡配置跨域請求
跨域問題報錯
解決
- 在vue.config.js中配置
module.exports = {
// 配置跨域請求
devServer: {
port: 8080,
host: 'localhost',
open: true,
https: false,
proxy: {
'/api': {
target: 'http://需要請求的地址/',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
在環境變數中或者在baseURL中更改地址值為’/api’
-.env.development
VUE_APP_API_URL= /api
-.env.production
VUE_APP_API_URL= /api