Vue devServer代理无济于事,我仍然收到CORS错误

问题描述 投票:4回答:1

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

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

但是我不断收到CORS错误:

从原始位置访问“ http://localhost:8080/api”处的XMLHttpRequest“ http://localhost:3000”已被CORS政策阻止:否请求中存在“ Access-Control-Allow-Origin”标头资源。

任何想法?

vue.js webpack webpack-dev-server vue-cli-3
1个回答
0
投票

看来问题出在axios配置上。

我有这个定义:axios.defaults.baseURL = "http://localhost:8080/api";我将其更改为axios.defaults.baseURL = "api";

并且有效。

module.exports = {
    ...
    devServer: {
        proxy: {
          "^/api": {
          target: url,
          ws: true,
          changeOrigin: true
        }
     }
  },
}
© www.soinside.com 2019 - 2024. All rights reserved.