最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send jwt in xmlhttprequest? - Stack Overflow

programmeradmin3浏览0评论

I can store the jwt token in local storage, but i don't know how to send it as a bearer token back to my restful api endpoint and i just can't find this information anywhere. How could i do that using only xmlhttprequest?

I can store the jwt token in local storage, but i don't know how to send it as a bearer token back to my restful api endpoint and i just can't find this information anywhere. How could i do that using only xmlhttprequest?

Share Improve this question asked Oct 7, 2018 at 3:30 lucaspcblucaspcb 812 silver badges6 bronze badges 1
  • 1 xhr.setRequestHeader('Authorization', 'Bearer ' + jwtoken); – Jaromanda X Commented Oct 7, 2018 at 3:32
Add a ment  | 

3 Answers 3

Reset to default 4

This can be done by using setRequestHeader() function. This function is using to set the value of an HTTP request header. When using setRequestHeader(), you must call it after calling open(), but before calling send(). If this method is called several times with the same header, the values are merged into one single request header.

function initXMLHttpRequest(method, url, jwtoken){
    let xmlHttpRequest = new XMLHttpRequest();
    xmlHttpRequest.open(method, url);
    xmlHttpRequest.setRequestHeader('Authorization', 'Bearer ' + jwtoken);
    return xmlHttpRequest;
}

It could be possible.

xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwtoken"))

Set authorization header in your xmlhttprequest.

req.setRequestHeader("Authorization", token);
发布评论

评论列表(0)

  1. 暂无评论