[英]Not able to access cookie in javascript at path /
I am using jQuery to access/set the cookies. 我正在使用jQuery访问/设置cookie。 I have planted a cookie named
CookieNo1
at path /
. 我在路径
/
处植入了一个名为CookieNo1
的cookie。
I planted this using the url localhost:8080/audi
. 我使用url
localhost:8080/audi
种植了它。
The cookie value is stored, which I checked manually on the firefox cookies. cookie值已存储,我在Firefox cookie上手动检查过。 Now when I try to access the same cookie, using the url
localhost:8080/audi/products
using $.cookie('CookieNo1');
现在,当我尝试访问相同的cookie时,使用
$.cookie('CookieNo1');
使用url localhost:8080/audi/products
$.cookie('CookieNo1');
This doesn't seem to retrieve the value of the cookie. 这似乎没有检索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. 但是,当我尝试使用相同的
localhost:8080/audi/products
url编写cookie时,它将覆盖以前的cookie值。 Please help me with this issue. 请帮我解决这个问题。
All I need is $.cookie('CookieNo1')
to return the previous cookie value instead of null. 我需要的只是
$.cookie('CookieNo1')
返回以前的cookie值,而不是null。 Thanks in advance 提前致谢
You have to set the expiry date. 您必须设置有效期限。 Otherwise, the cookie is removed at the end of the session.
否则,cookie将在会话结束时被删除。 In JQuery:
$("CookieNo1", "value", {expires: 7})
(this cookie stays for 7 days). 在JQuery中:
$("CookieNo1", "value", {expires: 7})
(此cookie保留7天)。
In JavaScript: 在JavaScript中:
document.cookie = "CookieNo1=value; max-age=604800";
max-age
sets the maximum lifetime of a cookie, in seconds. max-age
设置cookie的最长生存时间(以秒为单位)。
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 });
@RobW我在页面http:// localhost:8080 / audi上使用jquery添加了cookie,其代码
$.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');'
我尝试使用
'$.cookie('asdftraffic');'
从URL http:// localhost:8080 / audi / products检索cookie'$.cookie('asdftraffic');'
which returns null.返回null。
Your issue is caused by secure: true
. 您的问题是由
secure: true
引起的secure: true
。 This attribute requires the cookie to be transmitted over a secure connection ( https
). 此属性要求cookie通过安全连接(
https
)传输。 Remove the secure: true
flag if you're not using an encrypted connection. 如果您不使用加密连接,请删除
secure: true
标志。
First you set the cookie: 首先,您设置cookie:
var myvalue = 100, 2000, 300;
$.cookie("mycookie", myvalue);
Then you get the cookie: 然后,您获得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 ie when the browser is closed.
如果未指定到期时间,则在会话结束时即关闭浏览器时删除cookie。
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. 哦,会话会在关闭浏览器时结束,而不是在页面卸载时结束,因此会话cookie会完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.