jquery篮子如何保存数据
我正在创建一个小型网上商店。当我将产品添加到我的购物篮时,我只需将一个数据对象添加到 div 中,如下所示:
$("#ArticlesHolder").data('15', {name:'testname', nr:'4', price:'400', articlenr:'klr441234'});
只要我不重新加载页面,这种方法就很好用。我的问题是,jquery 中缓存此数据的最佳方法是什么,以便我可以重新加载页面、将页面保留 30 分钟以及您可以想出的其他内容。是向用户浏览器添加 cookie 还是 jquery 有其他方法来处理这个问题?感谢您的帮助。顺便问一下,如果 cookie 是解决方案,那么如果用户不接受 cookie 会发生什么?
编辑
如果用户不接受,尝试使用 cookie,然后对 web 服务进行 ajax 调用以在服务器端处理它并返回 json 数据,这是否是一个好的解决方案?
最终编辑
好的,所以我将使用 cookie。现在我如何将 JSON 数据存储在 cookie 中?正如您在上面的代码行中看到的那样,我可能有许多具有多个值的产品。像这样:
$("#ArticlesHolder").data('15', {name:'testname', nr:'4', price:'400',articlenr:'345345'});
$("#ArticlesHolder").data('25', {name:'name2', nr:'1', price:'100', articlenr:'kltt444'});
$("#ArticlesHolder").data('37', {name:'name3', nr:'14', price:'60', articlenr:'1235555'});
我知道如何需要循环每个数据字段并将其保存在新的 JSON 数据类型中。有人知道这是怎么做到的吗?
此致 马丁
Im creating a small webshop. When I add products to my basket i simply add a data object to a div li this:
$("#ArticlesHolder").data('15', {name:'testname', nr:'4', price:'400', articlenr:'klr441234'});
This works great as long as i dont reload a page. My question is this, whats the best way in jquery to cache this data so i can reload the page, leave the page for 30 min and other stuff you can come up with. Is it to add a cookie to the users browser or does jquery have some other way to handle this? Thx for any help. By the way, if cookies is the solution, what happens if the user doesnt accept cookies??
EDIT
Would it be a good solution to try and use a cookie, if the user dosnt accept that, then make a ajax call to a webservice to handle it server-side and return json data?
FINAL EDIT
Ok, so I will use cookies. Now how do i store a JSON data in a cookie? as you can se in the above code line i may have many products with multiple values. Like this:
$("#ArticlesHolder").data('15', {name:'testname', nr:'4', price:'400',articlenr:'345345'});
$("#ArticlesHolder").data('25', {name:'name2', nr:'1', price:'100', articlenr:'kltt444'});
$("#ArticlesHolder").data('37', {name:'name3', nr:'14', price:'60', articlenr:'1235555'});
I some how need to loop thrue each data field and save it in a new JSON data type. Anyone know how this is done?
Best Regards
Marthin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 是 JavaScript。
这意味着您被限制在当前页面视图内。
要将数据保存到网站,您可以
jQuery is javascript.
This means you are limited inside the current page view.
To persist data to the website, you can either
Cookie 是一种解决方案,但请记住,每次加载新页面时,Cookie 中存储的信息也会发送到服务器。
如果用户不接受cookies。这根本行不通。
未来,HTML5 Web 存储将有一个更好的解决方案在等着我们。
Cookies would be a solution, but keep in mind that with every new page load the info stored in the cookie is also send to the server.
If the user does not accept cookies. This will not work at all.
A much nicer solution will be waiting for us in the future with HTML5 web storage.
完整页面加载本质上会“重置”您的 JavaScript。浏览器不会在页面加载之间保存 JavaScript 状态。您可以选择创建 cookie 或将此信息保存在服务器状态中。这几乎是无状态网络的经典问题,它只是不会记住。
我建议结合使用 cookie 并将此缓存存储在服务器端。
A full page load is inherently going to "reset" your javascript. The browser will not hold javascript state between page loads. Your options are to create a cookie or to hold this information in server state. This is pretty much the classic issue of a stateless web, it just won't remember.
I suggest using a cookie in conjunction with storing this cache on the server side.
您必须使用 cookie 和服务器端解决方案,如果您希望使用 localStorage,我会选择 Lawnchair。
http://blog.westcoastlogic.com/lawnchair/
非常甜蜜。
You would have to use a cookie and a server side solution of If you'd like you use localStorage I'd go with Lawnchair.
http://blog.westcoastlogic.com/lawnchair/
It's pretty sweet.