【问题标题】:Vue devServer proxy is not helping, I still get CORS errorVue devServer 代理没有帮助,我仍然收到 CORS 错误
【发布时间】:2019-08-31 14:29:30
【问题描述】:

我正在使用@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”标头 资源。

有什么想法吗?

【问题讨论】:

  • 我不是 CORS 专家,但如果我猜我会说您需要从您的服务器启用 CORS 并将 Access-Control-Allow-Origin 添加到您的请求标头中。抱怨的是你的服务器,而不是你的 Vue 前端。
  • @JamesWhiteley - 这正是我定义代理的原因,所以我不需要在我的服务器上定义 CORS
  • @Tomer:你有没有找到解决这个问题的方法?
  • @dotNET - 实际上是的 :),显然我有一些忽略代理的 Axios 配置。我的开发服务器配置如下所示:devServer: { proxy: { "^/api": { target: url, ws: true, changeOrigin: true } } },

标签: vue.js webpack webpack-dev-server vue-cli-3


【解决方案1】:

看起来问题出在 axios 配置上。

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

它有效。

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

【讨论】:

  • 你在哪个文件中添加了这个axios.defaults.baseURL = "api"
  • 我在我的主文件中导入了 axios.config.js 文件,但你给它起什么名字并不重要
  • 是的,这非常有用。非常感谢
猜你喜欢
  • 2020-07-17
  • 1970-01-01
  • 2021-01-02
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多