Vue devServer 代理没有帮助,我仍然收到 CORS 错误

Vue devServer proxy is not helping, I still get CORS error

我正在使用@vue/cli 3.x,在我的 vue.config.js 中我有这个:

devServer: {
    proxy: {
      "/api": {
        ws: true,
        changeOrigin: true,
        target: "http://localhost:8080"
      }
    }
  }

但我不断收到 CORS 错误:

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 配置上。

我有这个定义:axios.defaults.baseURL = "http://localhost:8080/api"; 我改成了axios.defaults.baseURL = "api";

而且有效。

module.exports = {
    ...
    devServer: {
        proxy: {
          "^/api": {
          target: url,
          ws: true,
          changeOrigin: true
        }
     }
  },
}