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

10,339

It looks like the problem was with the axios configurations.

I had this definition: axios.defaults.baseURL = "http://localhost:8080/api"; I changed it to axios.defaults.baseURL = "api";

and it works.

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

Related videos on Youtube

Tomer
Author by

Tomer

Updated on October 14, 2022

Comments

  • Tomer
    Tomer over 1 year

    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"
          }
        }
      }
    

    But I keep getting CORS error:

    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.

    Any idea?

    • Tomer
      Tomer over 4 years
      @dotNET - actually yes :), apparently I had some Axios configurations that were ignoring the proxy. my dev server config looks like this: devServer: { proxy: { "^/api": { target: url, ws: true, changeOrigin: true } } },
  • anna
    anna over 4 years
    In what file do you exactly add this axios.defaults.baseURL = "api" ?
  • Tomer
    Tomer over 4 years
    I have axios.config.js file which i import in my main file, but it doesn't really matter what you name it
  • ibn_Abubakre
    ibn_Abubakre over 2 years
    Yup, this was very useful. Thanks a lot