Bruno Alex Bruno Alex - 14 days ago 6
Ajax Question

JSON to ver strage cache issue

Im retriving data from my php and its working fine , the problem is that

var myData
is getting cached and only updates if i refesh my page 2 times...

on my firebug i can see the post updates but when i `console.log(myData);the data is the old one ..until i refresh..

enter image description here

$.ajax({
url: "http://www.example.com/mobile_read.php", // path to remote script
dataType: "JSON",
type: 'post',
data:{
id : id,
eventos : eventos,

}, // data set to retrieve JSON
success: function (data) { // on success, do something...
// grabbing my JSON data and saving it
// to localStorage for future use.
localStorage.clear();
localStorage.setItem('myData1', JSON.stringify(data));
}
});

var myData = JSON.parse(localStorage.getItem('myData1'));

console.log(myData);

var arrayObjects = myData;

Answer

You're probably trying to set and read mydata before the request/response is complete. Instead, move that into your success callback.

$.ajax({
    url: "http://www.example.com/mobile_read.php",    // path to remote script
    dataType: "JSON", 
    type: 'post',
    data:{
    id : id,
    eventos : eventos,

},                               // data set to retrieve JSON
    success: function (data) {                       // on success, do something...
        // grabbing my JSON data and saving it
        // to localStorage for future use.
        localStorage.clear();
        localStorage.setItem('myData1', JSON.stringify(data));

        var myData = JSON.parse(localStorage.getItem('myData1'));
        console.log(myData);
        var arrayObjects = myData;
    }
});
Comments