如下如图,我想在二级域名之间实现cookie的跨域,但是以下代码无法实现,请问下各位大佬,我哪里写错了啊??
hosts:
127.0.0.1 cross1.cross.com127.0.0.1 cross2.cross.com
nginx:
#...省略server{ listen 80; server_name cross1.cross.com; location / { root e:\/cross1; index index.html; } } server{ listen 80; server_name cross2.cross.com; location / { root e:\/cross2; index index.html; } }#...省略
cross1/index.html
<script> document.domain ='cross.com'; document.cookie="name=value";</script>
cross2/index.html
<script> document.domain ='cross.com'; console.log(document.cookie,'cross2');</script>
这是cross1.html的cookie
这是cross2.html的打印,可以看到没有任何显示,就是无法获取到cross1/index.html设置的cookie啊,这个是咋回事呀?
解决方案
var cookieName = 'HelloWorld';var cookieValue = 'HelloWorld';var myDate = new Date();myDate.setMonth(myDate.getMonth() + 12);document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate + ";domain=.example.com;path=/";