1

i'm currently struggling with this one, my localstorage key value doesn't update the value when ajax success and remains the old stored data, however it updates only when i press f5 or i refresh the page which is not friendly.

var hide_cat = $('#hide_cat').val();
function fetch_data(){
$.ajax({
    url: '../ajax/ajax_fetch_loop.php',
    data : {hide_cat : hide_cat},
    type: 'post',
    cache: false,
    success:function(data){
        localStorage.setItem('dat', data); /*<-- this  one should update new                        
                                                 value every time success,   
                                                 however its not working, it 
                                                 remians the old data and i 
                                                 need to refresh before it 
                                                 store a new value*/
    }
});
}
fetch_data();

function fetch_datas(){

//so after update i should get a new value after ajax success
var table_data = localStorage.getItem('dat');
$('#tboy_labasmo').html(table_data);
}
fetch_datas();  
Raffy T Lawrence
  • 315
  • 1
  • 6
  • 18
  • localStorage can store string only. So if you wish to store objects, you should use `JSON.stringify` – Rajesh Aug 07 '16 at 06:04
  • Possible duplicate of [Storing Objects in HTML5 localStorage](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) – Rajesh Aug 07 '16 at 06:04

1 Answers1

0

You need to use JSON.stringify before storing the response

localStorage.setItem("dat", JSON.stringify(data));
brk
  • 48,835
  • 10
  • 56
  • 78