首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vue js中向后端发送令牌?

如何在vue js中向后端发送令牌?
EN

Stack Overflow用户
提问于 2022-08-09 15:41:36
回答 2查看 149关注 0票数 1

我试着向后端付款,但是每次我发钱的时候,我都会从后端收到这条消息

代码语言:javascript
复制
{
    "success": false,
    "message": "No token Provided"
}

我的后端需要身份验证

这是我的脚本标签

代码语言:javascript
复制
 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>

当我检查开发工具时,我会看到我的令牌。

代码语言:javascript
复制
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ"

这是我的后端标题

代码语言:javascript
复制
 let token = req.headers["x-access-token"] || req.headers["authorization"];

请让我怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-09 18:48:46

您的代码看起来很好,只需创建一个对象,然后将其添加到url --我猜您是在寻找这样的东西。尝尝这个

代码语言:javascript
复制
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);
        });
    }
  }
票数 1
EN

Stack Overflow用户

发布于 2022-08-09 16:09:58

第一个参数是你的url

第二个参数是你的数据,

第三个参数是您的配置。

你可以像下面这样请求发邮件

代码语言:javascript
复制
axios
  .post(
    `http://localhost:5000/api/pay`, 
    data,
    {
      headers: {
        "Authorization": `Bearer ${token}` //mind the space before your token
        "Content-Type": "application/json",
        "x-access-token": token,
      }
    }
  );

注意:数据是您的请求主体。

埃克斯。

代码语言:javascript
复制
{
  "firstname": "Firat",
  "lastname": "Keler"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73294570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档