javascriptaxios

how can i send token to backend i n vue js


i'm trying to make payment to my backend, but each time i send the payment i get this message from my backend

{
    "success": false,
    "message": "No token Provided"
}

my backend requires authentication

this is my script tag

 methods: {
    sendTokenToServer(charge, response) {
      const token = localStorage.getItem("token");
      axios
        .post(`http://localhost:5000/api/pay`, {
          headers: {
            Authorization: "Bearer" + token,
            "x-access-token": token
          },
          totalPrice: this.getCartTotalPriceWithShipping,  
        })
        .then(res => {
          console.log(res);
        });
    }
  }
};
</script>

when i check my dev tool i see my token

token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ"

this is my backend headers

 let token = req.headers["x-access-token"] || req.headers["authorization"];

please how can i go about this


Solution

  • Your code looks fine, just create an object then add it to the url. I guess you're looking for something like this:

    methods: {
        sendTokenToServer(charge, response) {
     var request = {
            totalPrice: this.getCartTotalPriceWithShipping,
          };
          const token = localStorage.getItem("token");
          axios
            .post(`http://localhost:5000/api/pay`,request, {
              headers: {
                Authorization: "Bearer" + token,
                "x-access-token": token
              },
            })
            .then(res => {
              console.log(res);
            });
        }
      }