无法在路径 / 的 javascript 中访问 c​​ookie

发布于 2024-12-07 07:13:15 字数 514 浏览 0 评论 0原文

我正在使用 jQuery 来访问/设置 cookie。我在路径 / 处植入了一个名为 CookieNo1 的 cookie。

我使用 url localhost:8080/audi 植入了它。

cookie 值已存储,我在 firefox cookie 上手动检查了该值。现在,当我尝试访问同一个 cookie 时,使用 url localhost:8080/audi/products using $.cookie('CookieNo1');

这似乎并不检索 cookie 的值。它返回一个空值。但是,当我尝试使用相同的 localhost:8080/audi/products url 写入 cookie 时,它​​会覆盖以前的 cookie 值。请帮我解决这个问题。

我所需要的只是 $.cookie('CookieNo1') 返回之前的 cookie 值而不是 null。 提前致谢

I am using jQuery to access/set the cookies. I have planted a cookie named CookieNo1 at path /.

I planted this using the url localhost:8080/audi.

The cookie value is stored, which I checked manually on the firefox cookies. Now when I try to access the same cookie, using the url localhost:8080/audi/products using $.cookie('CookieNo1');

This doesn't seem to retrieve the value of the cookie. It returns a null value. However when I try to write the cookie using the same localhost:8080/audi/products url, it overwrites the previous cookie value. Please help me with this issue.

All I need is $.cookie('CookieNo1') to return the previous cookie value instead of null.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

短叹 2024-12-14 07:13:15

您必须设置到期日期。否则,cookie 将在会话结束时被删除。在 JQuery 中:$("CookieNo1", "value", {expires: 7}) (此 cookie 保留 7 天)。

在 JavaScript 中:

document.cookie = "CookieNo1=value; max-age=604800";

max-age 设置 cookie 的最长生命周期(以秒为单位)。

编辑

引用评论:

@RobW我在页面上使用jquery添加了cookie
http://localhost:8080/audi ,代码为 $.cookie("asdftraffic",
valueToSet, { expires: 30, path: '/', secure: true });
我尝试
从 URL http://localhost:8080/audi/products 检索 cookie
使用 '$.cookie('asdftraffic');' 返回 null。

您的问题是由 secure: true 引起的。此属性要求通过安全连接 (https) 传输 cookie。如果您不使用加密连接,请删除 secure: true 标志。

You have to set the expiry date. Otherwise, the cookie is removed at the end of the session. In JQuery: $("CookieNo1", "value", {expires: 7}) (this cookie stays for 7 days).

In JavaScript:

document.cookie = "CookieNo1=value; max-age=604800";

max-age sets the maximum lifetime of a cookie, in seconds.

EDIT

Quote from comments:

@RobW I added the cookie using jquery on the page
http://localhost:8080/audi with the code $.cookie("asdftraffic",
valueToSet, { expires: 30, path: '/', secure: true });
I try to
retrieve the cookie from the url http://localhost:8080/audi/products
using '$.cookie('asdftraffic');' which returns null.

Your issue is caused by secure: true. This attribute requires the cookie to be transmitted over a secure connection (https). Remove the secure: true flag if you're not using an encrypted connection.

梦里人 2024-12-14 07:13:15

首先你设置cookie:

var myvalue = 100, 2000, 300;
$.cookie("mycookie", myvalue);

然后你得到cookie:

var getmycookie = $.cookie("mycookie");
    var myvalues = getmycookie.split(",");
    var firstval = myvalues[0];
    var secondval = myvalues[1];
    var thirdval = myvalues[2];

应该不会太难。
如果未指定过期时间,则 cookie 将在会话结束时(即浏览器关闭时)被删除。

编辑:您还可以指定路径:

$.cookie("mycookie", myvalue, {
expires : 10,           //expires in 10 days

path    : '/products',          //The value of the path attribute of the cookie 
                       //(default: path of page that created the cookie).

domain  : 'http://localhost:8080',  //The value of the domain attribute of the cookie
                       //(default: domain of page that created the cookie).

secure  : true          //If set to true the secure attribute of the cookie
                       //will be set and the cookie transmission will
                       //require a secure protocol (defaults to false).
});

我认为类似这样的事情会做:

var myvalue = 100, 2000, 300;
$.cookie("mycookie", myvalue, {path : '/audi/products'});

哦,会话在浏览器关闭时结束,而不是在页面卸载时结束,所以会话 cookie 就可以了。

First you set the cookie:

var myvalue = 100, 2000, 300;
$.cookie("mycookie", myvalue);

Then you get the cookie:

var getmycookie = $.cookie("mycookie");
    var myvalues = getmycookie.split(",");
    var firstval = myvalues[0];
    var secondval = myvalues[1];
    var thirdval = myvalues[2];

Should'nt be much harder.
When not specifying an expiration, the cookie is deleted on session end i.e. when the browser is closed.

EDIT: You can also specify the path:

$.cookie("mycookie", myvalue, {
expires : 10,           //expires in 10 days

path    : '/products',          //The value of the path attribute of the cookie 
                       //(default: path of page that created the cookie).

domain  : 'http://localhost:8080',  //The value of the domain attribute of the cookie
                       //(default: domain of page that created the cookie).

secure  : true          //If set to true the secure attribute of the cookie
                       //will be set and the cookie transmission will
                       //require a secure protocol (defaults to false).
});

I would think something like this would do:

var myvalue = 100, 2000, 300;
$.cookie("mycookie", myvalue, {path : '/audi/products'});

Oh, and a session ends when the browser is closed, not when the page is unloaded, so a session cookie will do.

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