最近在ie11 (Internet explorer 11)中碰到一个问题,在写入cookie的时候,控制台输出 document.cookie 值都为“” .
这点让我很疑惑,后来在stackflow上找到解决方案,与大家分享下。
处理方案:
1.首先得确认IE的Internet选项:让我们打开 设置-->internet选项
document.cookie = "token=foobar; expires=Tue, 09-Apr-2016 01:01:01 GMT; path=/; domain=localhost; httponly"
选择隐私-->高级 选项
将对应的cookie 选项勾选上,这样IE浏览器对cookie的拦截 程度就会变低。
到这一步 你可以观察一下问题是否解决,如果未解决,可以看下一步的解决方法。
当时我对cookie的 写入代码如下
document.cookie = "token=foobar; expires=Tue, 09-Apr-2016 01:01:01 GMT; path=/; domain=localhost; httponly"
写入一直失败 document.cookie一直为"" 前面已经按照internet选项设置。
后面发现 bebbo 在stackoverflow的解答
IE11 does accept a domain but it does not accept domains of
localhost
or machine names without a TLD likehttp://myservername/
. It also apparently chokes on subdomains with underscores翻译为 IE11 cookie可以设置 domain 但是不能接受localhost 或者 顶级域名 类如http://myservername/,同时也不接受带下划线的子域名(_talck.com)
具体文章见 https://weblog.west-wind.com/posts/2012/Apr/25/Internet-Explorer-and-Cookie-Domains
因此 去掉 domain 或者设置为网址 问题解决!
document.cookie = "token=foobar; expires=Tue, 09-Apr-2016 01:01:01 GMT; path=/;httponly"
以上内容 如有出错,或者表达错误,欢迎指出