最近在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"
以上內容 如有出錯,或者表達錯誤,歡迎指出