Vue devServer 代理没有帮助,我仍然收到 CORS 错误 [英] Vue devServer proxy is not helping, I still get CORS error

查看:59
本文介绍了Vue devServer 代理没有帮助,我仍然收到 CORS 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆