无法删除与AngularJS的$饼干饼干 [英] Can't delete cookie with AngularJS's $cookies

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

问题描述

我的web应用程序是由这样当服务器中的用户登录添加了一个Set-Cookie头的响应,是这样的:

My web app is made so that when a user logs in the server adds a Set-Cookie header to the response, like this:

设置Cookie:JSESSIONID = 1;路径= /对myApp /;安全

在注销我尝试删除客户端(浏览器),此cookie,因为我如果会议在服务器上成功摧毁只要该cookie被删除不关心你。任何挥之不去的幽灵会议将不时清理时间在服务器上。

On logout I try to delete this cookie on the client (browser), as I don't care if the session was successfully destroyed on the server as long as the cookie is deleted. Any lingering "ghost" sessions will be cleaned up from time to time on the server.

不过,我的应用程序是无法删除的cookie JSESSIONID。说登出功能从 https://test.myserver.com/myApp/app/index.html#/mySubPage 打电话注销功能所做的:

However, my app is unable to delete the JSESSIONID cookie. Say the logout function was called from https://test.myserver.com/myApp/app/index.html#/mySubPage and the logout function does:

delete $cookies["JSESSIONID"];
$location.path("/login");

该Cookie不会被删除。在Chrome开发者工具的资源选项卡刷新饼干的清单显示它仍然存在。重新加载登录页面,在资源选项卡刷新饼干上市仍显示该cookie。

The cookie isn't deleted. Refreshing the cookies listing in the "Resources" tab in Chrome Developer Tools shows it's still there. Reloading the login page and refreshing the cookies listing in the "Resources" tab still shows the cookie.

我为什么不能从我的JavaScript客户端删除cookie时,它不是一个中HTTPOnly饼干吗?难道这是造成问题的路径?不应作为脚本被包括在cookie的路径的页上运行。 Cookies是不是真的那么难以对付正常,所以我很清楚,有可能是一些小事,我在这里可以俯瞰 - 但任何帮助将是非常美联社preciated

Why can't I delete the cookie from my Javascript client when it's not a HTTPOnly cookie? Is it the path that's causing problems? Shouldn't be, as the script is running on a page included in the cookie's path. Cookies aren't really that difficult to deal with normally, so I'm well aware that there might be something trivial I'm overlooking here - but any help would be much appreciated.

更新:

我给在我原来的职位在错误的道路,以我的应用程序。它现在编辑,以反映正确的路径(当然,抽象)。事实证明,这是问题的关键信息。 AngularJS使用该应用程序的完整的相对路径,因为它创造所有Cookie /路径属性删除,这样以来我们的服务器设置与 /对myApp / 的路径的饼干和应用程序是 /对myApp /应用的相对路径上运行,角试图删除以前的cookie,它不存在(以覆盖或删除现有的cookie名称,域和路径都必须完全相同创建的cookie时使用)。

I had given the wrong path to my app in my original post. It's now edited to reflect the correct path (well, abstractly). It turns out that this was critical information for the question. AngularJS uses the complete relative path of the app as the path attribute of all cookies it creates/deletes, so since our server set the cookie with a path of /myApp/ and the app was running on the relative path of /myApp/app, Angular was trying to delete the former cookie, which doesn't exist (in order to overwrite or delete an existing cookie the name, domain, and path all need to be identical to those used when creating the cookie).

推荐答案

我想你应该检查从服务器的响应 - 也许他们包括设置cookie的值设置cookie的头字段

I suppose you should check the responses from server - maybe they include a 'set cookie' header field that sets the cookie's value.

下面是一个 Plunker例如,说明如何添加/修改/删除/监视cookie的值(但没有服务器端的响应,可以改变cookie值)。

Here is a Plunker example that illustrates how you can add/change/delete/watch for cookie's value (but without server-side responses that can change cookie values).

但是总体来说,$饼干对象是一种代理对象是由AngularJS ngCookies模块,并从一个侧面改变其领域跟踪双向时,浏览器的cookie被改变(所以你可以$监视更改),而且还改变了反映到浏览器真实的饼干,这样你就可以改变这个对象。

But generally, $cookies object is kind of 'proxy object' that is two-way tracked by AngularJS ngCookies module and from one side change its fields when browser's cookies are changed (so you can $watch for changes) but also changes are reflected to browser's real cookies, so you can change this object.

那么,为什么饼干不能被删除的唯一原因是你删除他们的浏览器和服务器重新设置cookie,并刷新页面后,它仍然是存在的。

So the only reason why cookies cannot be deleted is that you delete them on browser, and server sets the cookie again and after reloading page it is still there.

这篇关于无法删除与AngularJS的$饼干饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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