简体   繁体   English

为什么有些cookie无法从document.cookie获得?

[英]Why some cookie cannot get from document.cookie?

I'm using document.cookie go get cookie value of website, but it cannot get all cookie values. 我正在使用document.cookie去获取网站的cookie值,但是它无法获取所有cookie值。 Example session cookie sid , I can see it in Google Chrome Cookie Manager, but cannot get value by javascript. 示例会话cookie sid ,我可以在Google Chrome Cookie Manager中看到它,但无法通过javascript获取价值。

How I can set cookie by javascript but it does not display in document.cookie (still send these value to server in request header)? 我如何通过JavaScript设置cookie,但它不会显示在document.cookie (仍然将这些值发送到请求标头中的服务器)?

Answer copied from github: https://github.com/expressjs/session/issues/274#issuecomment-185308426 从github复制的答案: https : //github.com/expressjs/session/issues/274#issuecomment-185308426

Your cookie is likely set to httponly: true. 您的cookie可能设置为httponly:true。 This is the default value. 这是默认值。 If you, or anyone else reading this doesn't already know, it can be unnecessary and a bad decision to set this value to false. 如果您或其他未读过此内容的人不知道,则可能没有必要,并且将此值设置为false的错误决定。 Search for "httponly cookie" and you'll find some good explanations of why you wouldn't want Javascript to have access to cookies. 搜索“ httponly cookie”,您会找到一些很好的解释,说明为什么您不希望Javascript访问cookie。

Also make sure that the cookie you are trying to access is in the scope of the document from where you are trying to access the cookie. 另外,请确保您尝试访问的cookie在您尝试访问该cookie的文档的范围内。

The Domain and Path directives define thescope of the cookie: what URLs the cookies should be sent to. 域和路径指令定义cookie的范围:应将cookie发送到哪些URL。

Domain specifies allowed hosts to receive the cookie. 域指定允许主机接收cookie。 If unspecified, it defaults to the host of the current document location, excluding subdomains. 如果未指定,则默认为当前文档位置的主机,不包括子域。 If Domain is specified, then subdomains are always included. 如果指定了域,则总是包含子域。

For example, if Domain=mozilla.org is set, then cookies are included on subdomains like developer.mozilla.org. 例如,如果设置了Domain = mozilla.org,则cookie会包含在子域中,例如developer.mozilla.org。

Path indicates a URL path that must exist in the requested URL in order to send the Cookie header. 路径表示在URL中必须存在的URL路径才能发送Cookie标头。 The %x2F ("/") character is considered a directory separator, and subdirectories will match as well. %x2F(“ /”)字符被视为目录分隔符,并且子目录也将匹配。

For example, if Path=/docs is set, these paths will match: 例如,如果设置了Path = / docs,则这些路径将匹配:

/docs /文档

/docs/Web/ /文档/网络/

/docs/Web/HTTP /文档/网络/ HTTP

source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Scope_of_cookies 来源: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Scope_of_cookies

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM