PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 13, 2022

[FIXED] how can i send token to backend i n vue js

 October 13, 2022     axios, javascript     No comments   

Issue

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 your looking for something like this.. try 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);
        });
    }
  }


Answered By - jibzy
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing