我们正在尝试设置 cookie 以便使用用户自动登录。
我们正在使用带有 Reactjs + Redux + JavaScript (ES6) 的 SPA,
为了设置 cookie,我们创建了一个名为 CookieHandler
的组件,其中包含设置 cookie 函数
setCookie(token = '', expirationDate = '1970-01-01T00:00:00') {
const expDay = new Date(expirationDate);
document.cookie = 'userToken=' + token + '; expires=' + expDay.toUTCString() + '; path=/;';
}
我们还确保仅在需要时调用一次。
这适用于 Internet Explorer 中期望的所有浏览器。
问题是 IE 在当前 session 中设置了 token ,但是一旦我们关闭窗口并重新打开它,cookie 就消失了,我也尝试过使用 toGMTString
(已弃用)而不是 toUTCString
但仍然不起作用
额外
我们从后端获取userToken
和expirationDate
,其格式与setCookie
函数中的默认值相同
即使我们关闭窗口,在所有其他浏览器中也能按预期工作。
最佳答案
Here我发现一些信息表明 IE 不喜欢“=”符号。 也许这就是问题所在?
Don't use "=" signs in your cookie names.
If you need them, use the single quotes to tell IE not to interpret it, but to accept it as a literal.
关于javascript - 互联网浏览器 : expiration date for cookies doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747420/