简体   繁体   English

JavaScript 域从子域读取 cookie

[英]JavaScript domain read cookie from subdomain

I have a simple question - is there any way to read subdomain cookie on main domain by JavaScript?我有一个简单的问题 - 有没有办法通过 JavaScript 读取主域上的子域 cookie? For example: I set cookie 'hello' with value '2' in subdomain yeah.something.com and after that I want to read it on something.com.例如:我在子域yeah.something.com 中将cookie 'hello' 设置为'2',然后我想在something.com 上阅读它。 I tried to find answer for that but I found only information how to set cookie on subdomain by creating it on domain.我试图找到答案,但我只找到了如何通过在域上创建它来在子域上设置 cookie 的信息。

An ancient question, but better to have it answered You can do it by creating a cookie on the domain with a dot on the start to support reading from all subdomains and from the main domain as well:一个古老的问题,但最好回答它您可以通过在域上创建一个带有点的 cookie 来支持从所有子域和主域读取:

document.cookie = "_id=MY_ID;domain=.example.com;expires=;SameSite=none;Secure";

Hope it will help other lookers like me :)希望它能帮助像我这样的其他人:)

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

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