Vue devServer 代理没有帮助,我仍然收到 CORS 错误 [英] Vue devServer proxy is not helping, I still get CORS error
问题描述
我正在使用 @vue/cli 3.x 并且在我的 vue.config.js 中我有这个:
I'm using @vue/cli 3.x and in my vue.config.js I have this:
devServer: {
proxy: {
"/api": {
ws: true,
changeOrigin: true,
target: "http://localhost:8080"
}
}
}
但我不断收到 CORS 错误:
But I keep getting CORS error:
在 'http://localhost:8080/api' 从源访问 XMLHttpRequest'http://localhost:3000' 已被 CORS 策略阻止:否请求中存在Access-Control-Allow-Origin"标头资源.
Access to XMLHttpRequest at 'http://localhost:8080/api' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
有什么想法吗?
推荐答案
看起来问题出在 axios 配置上.
It looks like the problem was with the axios configurations.
我有这样的定义:axios.defaults.baseURL = "http://localhost:8080/api";
我把它改成 axios.defaults.baseURL = "api";
I had this definition: axios.defaults.baseURL = "http://localhost:8080/api";
I changed it to axios.defaults.baseURL = "api";
它有效.
module.exports = {
...
devServer: {
proxy: {
"^/api": {
target: url,
ws: true,
changeOrigin: true
}
}
},
}
这篇关于Vue devServer 代理没有帮助,我仍然收到 CORS 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!