我试着向后端付款,但是每次我发钱的时候,我都会从后端收到这条消息
{
"success": false,
"message": "No token Provided"
}我的后端需要身份验证
这是我的脚本标签
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>当我检查开发工具时,我会看到我的令牌。
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ"这是我的后端标题
let token = req.headers["x-access-token"] || req.headers["authorization"];请让我怎么做?
发布于 2022-08-09 18:48:46
您的代码看起来很好,只需创建一个对象,然后将其添加到url --我猜您是在寻找这样的东西。尝尝这个
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);
});
}
}发布于 2022-08-09 16:09:58
第一个参数是你的url
第二个参数是你的数据,
第三个参数是您的配置。
你可以像下面这样请求发邮件
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,
}
}
);注意:数据是您的请求主体。
埃克斯。
{
"firstname": "Firat",
"lastname": "Keler"
}https://stackoverflow.com/questions/73294570
复制相似问题