I'm looking for the data of an API to log in to a page. To log in I need to use the POST method, but it returns me the data of the user already registered. So I am storing this data in a local storage for later composing the user data in the restricted page of this system. This is the first part of my code:
function loga() {
console.log("Enviando post");
let usuario = {
email: document.querySelector("#email").value,
senha: document.querySelector("#senha").value
};
let xhr = new XMLHttpRequest();
xhr.open("POST", "http://rest/logins", true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.addEventListener("load", function () {
console.log(xhr.responseText);
if (xhr.status == 200) {
window.location = "painel.html";
window.localStorage.setItem('usuario', xhr.responseText);
}
if (xhr.status == 500) {
var dadosInvalidos = document.querySelector('#dados-invalidos');
dadosInvalidos.classList.remove('invisivel');
}
});
xhr.send(JSON.stringify(usuario));
}
So far so good, the page is already able to login and I retrieve this data on the restricted page of the user. But I still can only see this data in the console of my browser.
I tried to make another code, in another js file, to retrieve the data which is this:
var usuario = window.localStorage.getItem('usuario');
usuario.responseText = JSON.parse.responseText;
console.log(usuario);
var id = "";
id += usuario.id;
document.querySelector("#id").innerHTML = id;
But where it should appear the id only appears to me the word undefined. What do I need to do to display the id and other data on my page? Thanks to whoever respond.