user1332738 user1332738 - 1 month ago 7x
Ajax Question

How to store information from a ajax request

I have a jquery ajax function with a callback. This callback creates an array from a json that is retrieved from the function. So I have

success: function (response) {

and the callback creates the array with the call:


function createQuestionsArray(result){
for(var subject in result){
var size =Object.keys(result[subject]).length;
for(var i=0;i<size;i++){
questions[i] = result[subject][i];

I would like to load this array on the begining of the application so when the user start the quiz I don't need to load the questions. How can I store it? Because just putting in a global questions variable, when I go to another page I have an empty object.



You can storage on localStorage or sessionStorage, but you should note that both only save string, so you should first stringify the variable


for save

var data = JSON.stringify(yourarray);

for retrieve

var response = JSON.parse(localStorage.getItem('key'));

see a different between localStorage and sessionStorage

more info about session and local storage-