简体   繁体   English

重定向前删除cookie后无法检索javascript cookie

[英]Cannot retrieve javascript cookie after dropping cookie before redirect

My page HTML contains a span (with text) whose onClick = "goDropShip();" 我的页面HTML包含一个跨度(带有文本),其onClick = "goDropShip();"

The page contains a script that contains: 该页面包含一个脚本,该脚本包含:

function goDropShip() {

    var date = new Date();
    date.setTime(date.getTime()+(24*60*60*1000));
    document.cookie = "dropShip=true; expires="+date.toGMTString();
    window.location.href="http://www.domain.com";

}

I have verified, with " alert() ", that the function is in fact executing. 我已经使用“ alert() ”验证了该函数实际上正在执行。 And testing the cookie immediately after dropping it is successful. 删除Cookie后立即对其进行测试是成功的。 However, after the redirect, the cookie seems to be empty. 但是,重定向后,cookie似乎为空。

www.domain.com's index.html (which receives this redirect) has a script with the following: www.domain.com的index.html(接收此重定向)具有以下脚本:

if (document.cookie.indexOf("dropShip=true") >= 0  ) {
    window.location = "http://www.domain.com/processDropShip";
}

However, the cookie appears to be completely blank after the redirect. 但是,cookie重定向后似乎完全空白。

I am trying .indexOf() only because attempts to retrieve the cookie specifically by name have also failed, so I am trying to find the cookie in the entire document.cookie text. 我尝试使用.indexOf()只是因为尝试按名称专门检索cookie也失败了,所以我试图在整个document.cookie文本中查找cookie。

I am probably just not using the cookies properly, or I do not know how to. 我可能只是没有正确使用Cookie,或者不知道如何使用。

Forgive the fact that "dropShip" doesn't seem to make sense in this context, but I am having to obfuscate the details for security reasons. 原谅在这种情况下“ dropShip”似乎没有意义的事实,但是出于安全原因,我不得不混淆细节。 But the only thing I have changed is the cookie name. 但是我唯一更改的是cookie名称。

As always, thanks for any help. 与往常一样,感谢您的帮助。

如果您要设置Cookie的域和重定向到的域相同,则在添加Cookie时只需将路径设置为/即可在整个域中访问它。

document.cookie = "dropShip=true; path=/; expires="+date.toGMTString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM