$.ajax({ url: "get_cards.php", type: "GET", data: {selection:JSON.stringify(selection)}, success: function(data) { var json = JSON.parse(data); sessionStorage.setItem("json", JSON.stringify(json)); } });
Then in another file, I extract JSON from sessionStorage:
var json = JSON.parse(JSON.stringify(sessionStorage.getItem("json"))); if(json) { sessionStorage.removeItem("json"); }
This gives me an array of JSON objects, for example: [{'name':'Bob',...}]
. However, when I try to access the first element of the array: json[0]
, I get '['
, and when I try json[0].name
, I get undefined
. The length of json
is 159, so it counts every single character as an element.
EDIT: When I upgrade to:
var json = JSON.parse(sessionStorage.getItem("json")); if(json) { sessionStorage.removeItem("json"); }
I get length 1 (this is correct), but when accessing json[0].name
:
Uncaught TypeError: Cannot read property '0' of null at HTMLDocument.<anonymous> (studying.js:10) at j (jquery.min.js:2) at k (jquery.min.js:2)
source share