admin管理员组

文章数量:1321440

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);

本文标签: javascriptHow to send jwt in xmlhttprequestStack Overflow