为什么 jQuery cookie 实际上没有设置 cookie?

发布于 2024-12-29 19:16:54 字数 1181 浏览 1 评论 0原文

我正在使用 jQuery 开发一个使用 cookie 的应用程序。现在,它位于我的 PC 桌面上的 application.html 位置。

但是,我无法存储和检索 cookie。我在 HTML 文件中包含了 jquery-1.7.1.min.jsjson2.jsjquery.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 加密时,它看起来不错:

JSON test

但是,当我通过以下方式检索此 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:

JSON test

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2025-01-05 19:16:54

导致问题的原因可能是该文件位于您的桌面上。浏览器通常会根据接收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.

丢了幸福的猪 2025-01-05 19:16:54

如果您在使用 cookie 插件时遇到问题,为什么不编写自己的 cookie 函数呢?读取、写入和(可选)删除。

var createCookie = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
    }
    else var expires = '';
        document.cookie = name + '=' + value + expires + '; path=/';
};
var readCookie = function(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
};
var eraseCookie = function(name) {
    createCookie(name, '', -1);
};

我无法评论特定的插件,因为我从未使用过它。但是这些功能都可以工作并且已经过测试。

所以对于你的例子:

createCookie("people", JSON.stringify(people_obj_array), 7); // store
alert(readCookie("people")); // retrieve
eraseCookie("people"); // remove
alert(readCookie("people")); // oo look i'm no longer here.

If you are having troubles with the cookies plugin why not just make up your own cookie functions? Read, Write and (optional) delete.

var createCookie = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
    }
    else var expires = '';
        document.cookie = name + '=' + value + expires + '; path=/';
};
var readCookie = function(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
};
var eraseCookie = function(name) {
    createCookie(name, '', -1);
};

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:

createCookie("people", JSON.stringify(people_obj_array), 7); // store
alert(readCookie("people")); // retrieve
eraseCookie("people"); // remove
alert(readCookie("people")); // oo look i'm no longer here.
柠檬 2025-01-05 19:16:54

根据我的研究,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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文