Beusebiu
Beusebiu

Reputation: 1523

Vue application request wrong Axios baseURL

I try to create a baseURL from .env.local, but in request I get http://localhost:8083/Contracts/users insted of https://api.EXAMPLE.dev/api/users.

I use npm run serve -> http://localhost:8083

Any suggestion, please?

.env.locale

VUE_APP_ENDPOINT="https://api.EXAMPLE.dev/api"

main.js

import axios from "axios";

axios.defaults.baseURL = process.env.VUE_APP_ENDPOINT;

Component.vue

  created: function() {
    axios
      .get("/users")
      .then(response => {
        this.items = response.data;
      })
      .catch(error => {
        console.log(error);
      });
  },

Upvotes: 1

Views: 2884

Answers (2)

Beusebiu
Beusebiu

Reputation: 1523

Was no problem with the code, after a restart it worked.

npm run serve

Upvotes: 0

Abhishek Shastri
Abhishek Shastri

Reputation: 587

Try writing

axios.defaults.baseURL = process.env.VUE_APP_ENDPOINT || "https://api.EXAMPLE.dev/api"

And for Component.vue file

  created: function() {
    axios({
         url: "/users"
      })
      .then(response => {
        this.items = response.data;
      })
      .catch(error => {
        console.log(error);
      });
  }

Check if this works. May be that can be reason that process.env is not giving correct value.

Upvotes: 1

Related Questions