使用后退按钮进行部分 Ajax 更新
我正在使用 Ajax 对页面进行部分更新,这是一个购物车,当用户向其中添加商品时,它会使用 Ajax 进行更新。问题是,如果用户转到结账页面,然后点击后退按钮,购物车会看起来是空的,因为它是使用 Ajax 更新的。刷新页面使项目再次出现。
有没有办法强制更新后退按钮上的页面?
I'm doing partial update to the page with Ajax which is a shopping cart that updates with Ajax as users add items to it. The problem is that if users move to checkout and then hit the back button the shopping cart looks empty as it was updated with Ajax. Refreshing the page make items appear again.
Is there a way to force updating the page on back button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在ajax应用程序中使用history.pushstate,则可以将卡片的内容保存在全局命名空间中的变量中(例如window.cartItems = { item1, item2, ... }),并在按下后退按钮后然后再次呈现上一个按钮,从该全局对象中获取您的项目。
If you use the history.pushstate in your ajax app, you can keep the contents of the card in a variable in the global namespace (e.g. window.cartItems = { item1, item2, ... }), and after back button is pressed and the previous button is rendered again, get your items from that global object.
我建议不要改变后退按钮本身的行为。用户希望“后退”按钮能够返回上一页,而不是执行任何其他操作。
我建议采用不同的方法。例如,您可以有一段在购物车页面本身上运行的 JS。该脚本将从后端获取所有购物车数据。该脚本可以在页面加载时运行一次,确保购物车是最新的。
I would recommend against altering the behavior of the back button itself. Users expect the Back button to take them back a page, not to do anything else.
I would suggest a different approach. For example, you could have a piece of JS that runs on the cart page itself. This script would grab all the cart data from the backend. The script could run once when the page loads, ensuring that the cart is up-to-date.