为什么 jQuery cookie 实际上没有设置 cookie?
我正在使用 jQuery 开发一个使用 cookie 的应用程序。现在,它位于我的 PC 桌面上的 application.html
位置。
但是,我无法存储和检索 cookie。我在 HTML 文件中包含了 jquery-1.7.1.min.js
、json2.js
和 jquery.cookie.js
命令。
以下是我如何存储持续 7 天的 cookie:
$.cookie("people", JSON.stringify(people_obj_array), {expires: 7});
全局数组 people_obj_array
看起来像
[
{
"name": "Adam",
"age": 1,
},
{
"name": "Bob",
"age": 2,
},
{
"name": "Cathy",
"age": 3,
},
]
当我使用 alert(JSON.stringify(people_obj_array))
测试 JSON 加密时,它看起来不错:
但是,当我通过以下方式检索此 cookie 时:
alert($.cookie("people"));
在刷新页面之前,会弹出一个警报读 “无效的。”文本不应该是警报 JSON 字符串吗?我是否正确使用了 JQuery cookies 库?
只是为了澄清一下,这是我的测试方式:
$.cookie("people", JSON.stringify(people_obj_array), {expires: 7}); // store
alert($.cookie("people")); // attempt to retrieve
我有 Firebug,并且我愿意做一些控制台测试。
I am developing an application using jQuery that uses cookies. Right now, it is located at application.html
on my PC desktop.
However, I cannot store and retrieve a cookie. I had included jquery-1.7.1.min.js
, json2.js
, and jquery.cookie.js
in my HTML file in that order.
Here is how I am storing a cookie to last for 7 days:
$.cookie("people", JSON.stringify(people_obj_array), {expires: 7});
The global array people_obj_array
looks like
[
{
"name": "Adam",
"age": 1,
},
{
"name": "Bob",
"age": 2,
},
{
"name": "Cathy",
"age": 3,
},
]
When I test JSON encryption with alert(JSON.stringify(people_obj_array))
, it looks fine:
However, when I retrieve this cookie via:
alert($.cookie("people"));
before even refreshing the page, an alert pops up that reads "null." Shouldn't the text be the alert JSON string? Am I using the JQuery cookies library correctly?
Just to clarify, here is how I am testing:
$.cookie("people", JSON.stringify(people_obj_array), {expires: 7}); // store
alert($.cookie("people")); // attempt to retrieve
I have Firebug, and I am willing to do some Console tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
导致问题的原因可能是该文件位于您的桌面上。浏览器通常会根据接收cookie的域及其路径来提供cookie。
设置 cookie 后,您可能无法立即读取它:写入 cookie 涉及在 HTTP 请求中设置标头,同样,读取它们涉及读取 HTTP 响应中的标头。
尝试在网络服务器上托管您的页面,看看这是否适合您。
It's probably the fact the file is on your desktop that's causing the problem. Browsers normally behave by serving up cookies based on the domain they were received from and their path.
You may not be able to read the cookie immediately after setting it: Writing a cookie involves setting headers in a HTTP request and, likewise, reading them involves reading headers in a HTTP response.
Try hosting your page on a web-server and see if that works for you.
如果您在使用 cookie 插件时遇到问题,为什么不编写自己的 cookie 函数呢?读取、写入和(可选)删除。
我无法评论特定的插件,因为我从未使用过它。但是这些功能都可以工作并且已经过测试。
所以对于你的例子:
If you are having troubles with the cookies plugin why not just make up your own cookie functions? Read, Write and (optional) delete.
I cannot comment on the specific plugin as I have never used it.. however these functions all work and have been tested.
So for your example:
根据我的研究,
jquery.cookie.js
相当旧,并且似乎不再被维护。如果使用此库,您的运气可能会更好。它在 Google Code 上的描述是“具有 jQuery 绑定和 JSON 支持的 Javascript Cookie 库”,并且包含您想要做的所有事情的方法!From my research
jquery.cookie.js
is fairly old, and doesn't seem to be maintained any longer. You might have better luck using this library instead. Its description on Google Code is "Javascript Cookie Library with jQuery bindings and JSON support", and includes methods for everything you're trying to do!