簡體   English   中英

用javascript設置的cookie在刷新后消失

[英]Cookie set with javascript disappears after refresh

我正在這樣設置我的cookie:

document.cookie = `${cookieName}=${JSON.stringify(cookieObject)};path=/;SameSite=Strict;Secure=true;expires=${someDate.toUTCString()};`

一開始一切都很好。 但是當我刷新時,cookie 消失了,firefox 控制台告訴我一條消息:

Cookie “thecookiename” will be soon rejected because it has the “SameSite” attribute set to “None” or an invalid value, without the “secure” attribute.

如果我再次刷新,消息就會消失,cookie 也會消失。

Chrome 的行為相同,但減去了消息。

編輯

我意識到,問題可能與我正在覆蓋document.cookie設置器這一事實有關。

document.setCookie = document.__lookupSetter__('cookie');
document.newCookieSetter= (cookie) => {
    // here I check, whether a cookie is allowed or not. Calls for an api endpoint to get the information.

    if (cookieDataRecieved() && cookieIsAllowed(cookie)) {
        return document.setCookie(cookie);
    }
    cacheCookie(cookie); // If cookie data has loaded, try checking again.  
}

document.__defineSetter__('cookie', document.newCookieSetter);

該問題適用於通過document.cookie = "somecookie";添加的每個 cookie。

Secure屬性沒有值,它只是一個裸屬性(存在或不存在),因此只需將Secure=true替換為Secure

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM