0

根据 MDN,路径为 '/' 的 cookie可在同一域中的任何位置使用这得到了几个 不同 来源的证实。

但是,我只是没有发现这种情况。google.com我在使用 firefox时对(不存在的)目录进行了以下测试:

> 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
""

我是不是误会了什么?我究竟做错了什么?我在 Chrome 上尝试了类似的测试,结果相同。

4

1 回答 1

0

弄清楚了。我设置path的是逗号分隔符,而不是分号分隔符。它是在我输入时按字面意思编写 cookie,而不是解释路径和域。

编写 cookie 的正确方法是document.cookie = 'key1=value1;path=/'.

于 2018-06-25T12:28:53.510 回答