transport data to another page

0

I am doing the frontend of a login system and am using pure javascript for this. I can already log in to the API and in the browser console to receive the data of the user that is logging on. This is my AJAX:

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:web/rest/logins", true);
xhr.setRequestHeader("Content-type", "application/json");

xhr.addEventListener("load", function() {

console.log(xhr.responseText);

if( xhr.status == 200) {

  window.location="interno/index.php";
} 

if(xhr.status == 500) {

  var dadosInvalidos = document.querySelector('#dados-invalidos');
  dadosInvalidos.classList.remove('invisivel');
}
});
xhr.send(JSON.stringify(usuario));

}

The ones that appear to me on the console are the id and the rest of the user data. What I need to know now is how do I move the user id to the home page, that is, the page that comes after the user logs in, so I can build the user profile on it. How can I do this? Can someone help me? Thanks in advance.

    
asked by anonymous 16.01.2018 / 19:34

1 answer

1

You can carry information between pages using both sessionStorage and localStorage , but both have different behaviors. The sessionStorage holds the information as long as the browser tab / window remains open, while localStorage retains the information even after the browser is closed.

Both can be created using the setItem() method, eg

'sessionStorage.setItem("chave", "valor");'

To get the stored value you can use the getItem() method, eg

sessionStorage.getItem("chave");

If you need to store complex objects, you can use JSON.stringify() to store and then JSON.parse() to retrieve the value, eg:

var obj = { a: "1", b: "2" };
sessionStorage.setItem("meuObj", JSON.stringify(obj));
var objRecuperado = JSON.parse(sessionStorage.getItem("meuObj"));
    
16.01.2018 / 20:08