jQuery 存储会话数据
我正在尝试使用 jQuery AJAX。我的要求是,我希望从数据集中的数据库加载用户名,将其转换为 JSON 格式并将其存储在内存中或使用 jQuery 数据在用户浏览我的网站(即会话)时使用。这样我可以使用自动完成或我自己的代码向用户显示数据。
谁能帮我设计这样一个场景吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
HTML 5 Web 存储
超越 cookie、Google Gears 和专有解决方案
标准化,无需特殊插件。可通过 JavaScript/jQuery 编写脚本。虽然仅在最新的浏览器中受支持,但您可以使用 HTML 5 Web Storage,即 localStorage 和 sessionStorage 属性,旨在在页面请求之间的网站上下文中保留客户端的状态。能容纳的东西比饼干多得多。例如 IE 8 DOM 存储规范 (10 MB 客户端存储),Firefox DOM 存储规范。
HTML 5 Web Storage
think beyond cookies, Google Gears and proprietary solutions
Standardized, no special plugin required. Scriptable through JavaScript/jQuery. Although supported only in the latest browsers, you can use HTML 5 Web Storage, namely localStorage and sessionStorage properties, intended to keep state on the client side in context of the website between page requests. Holds a heck of a lot more than cookies. For example IE 8 DOM storage spec (10 MB client-side storage), Firefox DOM storage spec.
还有 YUI 2 Storage Utility,它可以为您抽象存储(HTML 5、Google Gears、SWF)取决于浏览器支持的内容:
There is also the YUI 2 Storage Utility which abstracts the storage for you (HTML 5, Google Gears, SWF) depending on what the browser supports:
JQuery 数据可能没有用,因为当您刷新页面从一个页面转到下一个页面时,您将丢失数据,但是您可以使用 COOKIES 来完成此操作 - 有一个 jQuery cookies 插件 这将使您更轻松。
JQuery data may not be useful because you will lose the data when you go from one page to the next as the page is refreshed, you can however do this using COOKIES -- there is a jQuery cookies plugin that will make it easier for you.
会话意味着服务器端。 jQuery 的意思是客户端。你无法在两者之间进行翻译。 Cookie 是您所能期望的最好的保持严格客户端的方式。
Sessions means serverside. jQuery means clientside. You can't translate between the two. Cookies is the best you can hope for to remain strictly clientside.
Google Gears 是我所知道的唯一解决方案,但它要求用户安装它并且仅在一些浏览器上运行。但是,当用户说...聚焦于自动完成的搜索框时,您可以使用
$.getJSON
从服务器获取 JSON 编码和 gzip 压缩的用户列表,然后使用激进的客户端资源缓存,以减少数据库的命中次数。如果它是一个相当长的用户列表,您可能需要考虑使用 memcached 来存储列表,或者可能将其写入文件并由前端服务器直接提供服务(即 Nginx、Lighttpd、Apache:很多避免运行数据库查询的事情)
Google Gears is the only solution I know of for doing this, but it requires the user to install it and only runs on a few browsers. You could, however, use
$.getJSON
to fetch a list of users, JSON-encoded and gzipped, from your server when the user say... focuses the auto-completed search box, then use aggressive client-side resource caching to reduce the number of hits on your database.If it's a pretty long list of users, you may want to look into using memcached to store the list, or perhaps writing it to a file and having it be served directly by your front-end server (i.e. Nginx, Lighttpd, Apache: pretty much anything that avoids running a DB query)
我一直在使用 PersistJS 并取得了巨大的成功,几乎完成了您所描述的工作。它是一个轻量级插件(< 10k),使用以下任何可用后端抽象出客户端存储:
它根据浏览器功能和安装的插件透明地选择最佳存储后端。它适用于:
...如果其他持久存储选项都不起作用,则可以选择回退到 cookie。 DOM 存储方法至少可以为您提供几兆字节的存储空间;如果您回退到 cookie,您的大小将被限制在大约 4kB。
I've been using PersistJS with great success to do pretty much what you're describing. It's a lightweight plugin (< 10k) that abstracts away client-side storage using any of these available backends:
It transparently picks the best storage backend based on browser capability and installed plugins. It works with:
...and optionally falls back to cookies if none of the other persistent storage options work. The DOM storage methods get you at least a few megabytes worth of storage space; you'll be limited to roughly 4kB if you fall back to cookies.