JSON到ver strage缓存问题

时间:2016-11-18 15:59:15

标签: javascript jquery json ajax

我从我的PHP中恢复数据及其工作正常,问题是

var myData正在缓存,只有更新我的页面2次才会更新...

在我的firebug上我可以看到帖子更新,但是当我`console.log(myData);数据是旧的..直到刷新..

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;

1 个答案:

答案 0 :(得分:0)

您可能在请求/响应完成之前尝试设置和读取mydata。相反,将其移至成功回调中。

$.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;
    }
});