carry data to another page

Asked

Viewed 318 times

0

I’m frontending a login system and I’m using pure javascript for it. I can already log into the browser API and console to receive the data from the user who is logging in. 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 carry the user id to the homepage, that is, the page that comes after the user logs in, and thus get the user profile mounted on it. How can I do that? Can anyone help me? Thanks in advance.

  • Take a look at sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

  • So @Maxrogério I’m half layman and still can’t read documentations, so I’m using the Forum. You know how to do this?

  • It has nothing to do with your doubt but, if you allow me an opinion: Pass user and password via http is not a good idea! Ever thought about encrypting using a public key and decrypting on the server? I usually do this using jsencrypt.

  • So... I just develop forntend, I develop for a JAVA API created by my nephew, and I haven’t been a frontend developer for a long time, I don’t really know what jsencrypt is, but thanks for the tip, I’ll look into what it is

1 answer

1


You can transport the information between pages using both sessionStorage as localStorage, but both have different behaviors. The sessionStorage keeps the information while the browser tab/window remains open, while the localStorage keeps information even after the browser is terminated.

Both can be created using the method setItem(), ex.:

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

To get the stored value you can use the method getItem(), ex.:

sessionStorage.getItem("chave");

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.