How to set and pick objects saved in Javascript session

Asked

Viewed 8,546 times

5

I’m wondering how to save an Array of objects in the session, and then how to recover them. Today I use the sessionStorage.setItem("pessoa", arrayPessoa); and sessionStorage.getItem("pessoa");, but it is giving null when I send it to the console.

How to solve this ?

  • You have a similar question here: http://answall.com/questions/72189/problema-com-vari%C3%A1vel-de-session, please check if the answer to this question helps you.

  • @Tiagooliveiradefreitas has nothing to do Session in php with sessionStorage in javascript

  • It’s @Tiagooliveiradefreitas as Gabriel said, has nothing to do.

  • Where does Session come from? PHP or Java?

  • @Giancarloabelgiulian actually I do a search in Indexeddb and I want to save the information in the session to be able to access them outside the function onsuccess.

  • Try this, request.getSession(). getattribute("person"), request.getSession(). setAttribute("person", value), but I don’t know if it works with array...

  • Only minute to test.

  • Uncaught Typeerror: Undefined is not a Function gave this error

  • It is worth noting that the programming is mobile

  • You can use sessionStorage but have to pass a string to it, or set the Person values one by one, or pass the array in string form

  • Make an answer for me to watch how you do

  • Cara to giving a shot in the dark, I’m new in programming, if it doesn’t work I don’t know what could work, I created an answer.

Show 7 more comments

2 answers

5


sessionStorage does not directly accept an object because it is created by key/value, a way to insert and then read an object is to string it with the JSON.stringify() function, then you can take Session using sessionStorage.getItem() and parse the value, example of how it could be implemented:

Example:

 var b = {'nome': 'Gabriel', 'sobrenome': 'Rodrigues'};
 b = JSON.stringify(b);
 sessionStorage.setItem('chave', b);
 var c = JSON.parse(sessionStorage.getItem('chave'));
 console.info(c);

See working on Jsfiddle

  • 1

    The result was this: Uncaught Syntaxerror: Unexpected token o

  • take a look at the working example: http://jsfiddle.net/gabrielr47/78kww75y/

  • If you give "Uncaught Syntaxerror: Unexpected token o" to you, it is because the object that is saved there in the sessionStorage is not the serialized, it is what you had already put before.

1

If you can create a JSON for the Person array:

{"Status":"OK!","Pessoa":
[{"IdPessoa":01,"Nome":"JOAO"},
 {"IdPessoa":02,"Nome":"MARIA"}
]}

Then pass JSON in String form:

var parsedResult = JSON.parse(result);
sessionStorage.setItem("Pessoa", JSON.stringify(parsedResult.Pessoa));

In this case JSON.parse(result), result is a dynamic JSON.

  • 1

    This was the same answer I put...

  • 1

    @Gabriel If you go to see, it was the same minute, so we typed practically at the same time. my 18:08:19, your 18:08:37

Browser other questions tagged

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