gpt4 book ai didi

Javascript:如何在每次ajax成功后更改localStorage Key的值

转载 作者:行者123 更新时间:2023-11-28 05:42:28 25 4
gpt4 key购买 nike

我目前正在努力解决这个问题,当ajax成功时,我的localstorage键值不会更新该值,并保留旧的存储数据,但是只有当我按f5或刷新不友好的页面时它才会更新。

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();

最佳答案

在存储响应之前,您需要使用JSON.stringify

localStorage.setItem("dat", JSON.stringify(data));

关于Javascript:如何在每次ajax成功后更改localStorage Key的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38811359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com