[英]How to fetch API with POST method and Authorization Bearer token in javascript?
var get = localStorage.getItem("token"); var get = localStorage.getItem("token"); var token = JSON.parse(get); var token = JSON.parse(get);
function myfun(){
fetch(url,{
method: 'POST',
headers:{
"Authorization":`Bearer + ${token}`
},
body:JSON.stringify({
city: account_city.value,
district: account_dis.value,
state: account_state.value,
})
})
.then((res)=> res.json())
.then(data=> console.log(data));
}
I try this code.我试试这段代码。 Please help!!!!请帮忙!!!!
If you use string interpolation from ES6, you don't need to add +
operator to make a concatenation.如果您使用 ES6 中的字符串插值,则不需要添加+
运算符来进行连接。
So try either: "Authorization": `Bearer ${token}`
or "Authorization": "Bearer " + token
所以尝试: "Authorization": `Bearer ${token}`
或"Authorization": "Bearer " + token
Try this, your code looks good, the only issue I am seeing is string Interpolation.试试这个,你的代码看起来不错,我看到的唯一问题是字符串插值。 What is the issue you are facing您面临的问题是什么
{
method: "POST",
url: url,
data: data,
headers: { Authorization: `Bearer ${token}` },
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.