[英]Cookie not available in different directory
According to MDN, cookies set with a path of '/' are available anywhere in the same domain . 根据MDN,设置为'/'路径的cookie 在同一域中的任何位置都可用 。 This is corroborated by several different sources .
几个 不同的 来源证实了这一点。
However, I just haven't found this to be the case. 但是,我只是没有发现这种情况。 I ran the following test on (nonexistent) directories on
google.com
using firefox: 我使用Firefox在
google.com
上的(不存在)目录上运行了以下测试:
> window.location = 'https://google.com/dir/file.html'
Navigated to https://google.com/dir/file.html
"https://google.com/dir/file.html"
> document.cookie
""
> document.cookie = 'key1=value1,path=/'
"key1=value1,path=/"
> document.cookie
"key1=value1,path=/"
> window.location = '/dir2/file.html'
Navigated to https://google.com/dir2/file.html
"/dir2/file.html"
> document.cookie
""
Am I misunderstanding something? 我误会了吗? What am I doing wrong?
我究竟做错了什么? I tried similar tests on Chrome with the same result.
我在Chrome上尝试了类似的测试,但结果相同。
Figured it out. 弄清楚了。 I was setting
path
with a comma delimiter, not a semicolon delimiter. 我使用逗号定界符而不是分号定界符设置
path
。 It was writing the cookie literally as I typed it, instead of interpreting the path and domain. 它是在我键入时按字面意思写Cookie,而不是解释路径和域。
The correct way to write the cookie would have been document.cookie = 'key1=value1;path=/'
. 编写cookie的正确方法是
document.cookie = 'key1=value1;path=/'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.