Skip to main content
EnZhoCR
Participating Frequently
February 19, 2023
Answered

JWT Authorization header

  • February 19, 2023
  • 1 reply
  • 971 views

Hello Community,

i need help with this problem.

 

I would like to use the : 

JWT Authorization header using the Bearer scheme. Example: "Authorization: Bearer {token}"

 

Someone can help me? I know, how can read an xml from a APi but without a header autentication. I need to know how can i pass trow the header this kind of token.

 

Thanks a lot

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    If you're using XMLHttpRequest, you could so something like this:

     

    var xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
    		console.log("request complete");
    };
    
    xhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwtoken"));
    xhttp.open("GET", "filename", true);
    xhttp.send();

     

     

    https://www.w3schools.com/xml/xml_http.asp

    https://stackoverflow.com/questions/52685215/how-to-send-jwt-in-xmlhttprequest

     

    Have you tried it?

     

    Please let us know.

     

    Regards,

    JC

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    February 20, 2023

    Hi.

     

    If you're using XMLHttpRequest, you could so something like this:

     

    var xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
    		console.log("request complete");
    };
    
    xhttp.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwtoken"));
    xhttp.open("GET", "filename", true);
    xhttp.send();

     

     

    https://www.w3schools.com/xml/xml_http.asp

    https://stackoverflow.com/questions/52685215/how-to-send-jwt-in-xmlhttprequest

     

    Have you tried it?

     

    Please let us know.

     

    Regards,

    JC

    EnZhoCR
    EnZhoCRAuthor
    Participating Frequently
    February 20, 2023

    Hello Joao, you already help me (i changed to a new account but in the past already asked and you answered) anyway thanks!

     i am trying to use something like this :

     

    var reqCurrent = new XMLHttpRequest();
    reqCurrent.open("GET", "URL", true);
    
    reqCurrent.setRequestHeader('Authorization', 'Bearer ' + 'Token');
    reqCurrent.addEventListener("load", currentLoader);
    reqCurrent.send();

     

    but i getting "CORS Policy error" , this is from server configuration or my fault?

    Thanks a lot, 

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 20, 2023

    Awesome! Really good to know!

     

    Have you tried something like this:

    xhttp.setRequestHeader("Access-Control-Allow-Origin", "*");

     

    If that doesn't work, then you probably have to configure the server.