gpt4 book ai didi

javascript - 在页面重新加载时保留 .html() 响应数据

转载 作者:行者123 更新时间:2023-12-01 02:02:32 25 4
gpt4 key购买 nike

我试图确保从 ajax 获得的数据传递到我的 View 中。我实际上取得了成功,但在页面刷新时数据消失了。这是我的 jquery 代码

$(document).ready(function(){

$('#Item').hide();

$('#submitButton').hide();

$("#cartIcon").click(function(){

var id = $('#id').val();

console.log(id);

$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST', // Type of response and matches what we said in the route
url: '/addtocart/', // This is the url we gave in the route
dataType: 'JSON',
data: {'id' : id}, // a JSON object to send back
success: function(response){ // What to do if we succeed
$('#subtotal').html(response.totalPrice);
$('#total').html(response.totalPrice);
$('#Item').show();
$('#noItem').hide();
$('#submitButton').show();
}
});
});
});

如何在页面重新加载后保留我的 .html 响应

最佳答案

将数据保存到cookie或local storage中。每次加载页面时,您都需要检查本地存储中是否有一些数据要显示。

关于javascript - 在页面重新加载时保留 .html() 响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50437966/

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