[Vuejs]-Error! Get request in Vuejs with axios (401 Unauthorized)

0👍

try this

./App.vue :

<template>
  <div id="app">
    <button @click="getStatesInfo">Click</button>
    <p>{{ states }}</p>
  </div>
</template>

<script>
import { getStates } from "./services";

export default {
  name: "App",
  data() {
    return {
      states: "",
    };
  },
  methods: {
    getStatesInfo() {
      getStates()
        .then((res) => {
          this.states = res;
          console.log(res);
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script>

./services/index.js :

const baseUrl = "http://localhost:4000/";
const axios = require("axios");
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 60000,
  headers: {
    "Authorization": "Bearer " + "your-token",
  },
});

export async function getStates() {
  const response = await instance.get("/states", {
    params: {
      id: "param1",
    },
  });

  return response.data;
}

Leave a comment