删除Cookie问题 [英] Remove Cookie issue

查看:122
本文介绍了删除Cookie问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cookie在用户登录www.example.com时保存令牌,然后根据他们使用的设备将其重定向到example.com/desktop或example.com/mobile。当他们注销桌面应用程序时,我将Cookie替换为null,然后在GWT中调用remove cookie,并将它们重定向到www.example.com,但cookie仍然存在。我在这里做错了吗?

I have a cookie save a token when a user logs into www.example.com and then it redirects them to example.com/desktop or example.com/mobile depending on what device they're using. When they log out of the desktop app I replace the cookie with null and then call remove cookie in GWT and redirect them to www.example.com, but the cookie still exists. Am I doing something wrong here? I haven't worked with cookies much before so I'm a bit new to this.

推荐答案

因为cookie被设置为另一个路径,您必须使用 Cookies.removeCookie(cookieName,/) / 是您的示例中使用的路径),而不是 Cookies.removeCookie(cookieName)

Because the cookie was set at another path, you have to use Cookies.removeCookie("cookieName", "/") (/ being the path used in your example) and not Cookies.removeCookie("cookieName").

这是因为没有指定的路径,路径默认为当前页面之一(请参阅 document.cookie )。

This is because without a specified path, the path defaults to the one of the current page (see document.cookie).

因此,您尝试在path = / desktop删除Cookie,实际上在路径= /,因此删除失败。

请记住,您可以有两个相同名称但路径不同的cookie;因此您可以在 path = / a 上建立 cookieName code> path = / desktop 。删除 path = / 下的cookie不会删除 path = / desktop 下的cookie,一个在 path = / desktop 将不会删除 path = /
作为附注:当访问 / desktop 时,浏览器会发送两个cookie,它们可能有不同的值。

So, you're trying to remove the cookie at path=/desktop, whereas it's actually at path=/, so the removal fails.
Remember that you could have two cookies with the same name but different paths; so you could have a cookieName at path=/ and a _cookieName at path=/desktop. Removing the cookie at path=/ won't remove the one at path=/desktop, and conversely, removing the one at path=/desktop won't remove the one at path=/. As a side note: when accessing /desktop, the browser would send both cookies, which could have different values.

简单来说,因为你设置你的cookie在 / ,记住总是传递 / ,无论你在哪里,或者你可以创建一个新的cookie而不是修改现有的,或无法删除它(你现在遇到的)。

In brief, because you set your cookie at /, remember to always pass / as the path, everywhere, or you could create a new cookie rather than modifying the existing one, or fail to remove it (which you're experiencing right now).

另请参见< a href =http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/Cookies.html#removeCookie%28java.lang.String,%20java.lang .String%29> Cookies.removeCookie(String,String)

这篇关于删除Cookie问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆