I then receive a reponse from the network with new informations
{Code: “SPTC2321326”, Name: “Bruno Mars”, Token: “randomNumbers”}
Now, I’m not sure how I can convert the JSON to an usable form in my html.
After some research, i saw that some people suggest to use JSON.parse, but their json data are always the same, while mine changes everytime.
What I think I’m supposed to do:
let response = '{"Code": "SPTC2321326", "Name": "Bruno Mars", "Token": "randomNumbers"}';
But I don’t know how to make it so that javascript takes the data returned by my request, since it changes everytime and I can’t write the value I get on one occasion. Sorry if I’m not completely clear.
Thanks you.
I guess I might be misunderstanding what you are trying to do here. I thought you said you receive a response back in JSON format which you need to parse to get the values of the Code, Name and Token properties. Is that correct?
Let’s pretend the following is the JSON response I receive back:
Hi,
I’m trying to send a header which will authorize me to access data.
function authentificationToken(obj){
console.log(obj);
let xhr = new XMLHttpRequest();
xhr.open('GET', 'http://website.com/messages');
xhr.setRequestHeader("Authorization", "Basic obj");
let chatText = JSON.parse(xhr.responseText)
}
The token is in obj, however, I don’t think I’m doing the authorization part correctly. My navigator doesn’t see my request, but my console line works.
Thanks you!
The object contain my token which changes every refresh, that’s why I used a variable for it, since it changes. If I can’t do that I don’t know what to use.
Well thanks, it worked. My mistake was that I did not have xhr.send();.
Now today, I’m trying to take the elements I received and put them into my HTML. Here’s what I tried:
function writeChatMessage(chatMessage){
console.log(chatMessage);
for (i = 0; i <= 50; i++) {
let chatSender = chatMessage[i].From;
let chatMessDate = chatMessage[i].Date;
let chatMessText = chatMessage[i].Text;
let chatWindow = document.getElementById('chat');
let ligne = chatWindow.createElement('DIV').outerHTML="<span class='chatDate'>"+chatMessDate+"</span>" +
"<span class='chatUser'>"+chatSender+"</span>" +
"<span class='chatMessage'>"+chatMessText+"</span>";
ligne.className ="ligneChat";
}
}
I think my issue is that I can’t use createElement like this in this situation, but basically I want to create a new div and include 3 inside of it with my data.
Thanks again!