Jas*_*ing 8 javascript c# asp.net cookies ajax
好的,这是411 - 我的Global.asax.cs文件中有以下事件处理程序:
private void Global_PostRequestHandlerExecute(object sender, EventArgs e)
{
if (/* logic that determines that this is an ajax call */)
{
// we want to set a cookie
Response.Cookies.Add(new HttpCookie("MyCookie", "true"));
}
}
Run Code Online (Sandbox Code Playgroud)
该处理程序将在Ajax请求期间运行(作为我正在使用的Ajax框架的结果),以及在其他时候 - if语句的条件过滤掉非Ajax事件,并且工作正常(它不相关)在这里,所以为了简洁起见我没有包含它.
我们可以说这很好用 - 设置了cookie,我能够在客户端上读取它,而且一切都很顺利.
现在对于让我疯狂的部分.
这是我用来删除cookie的JavaScript函数:
function deleteCookie(name) {
var cookieDate = new Date();
cookieDate.setTime(cookieDate.getTime() - 1);
document.cookie = (name + "=; expires=" + cookieDate.toGMTString());
}
Run Code Online (Sandbox Code Playgroud)
所以,当然,在设置cookie之后的某个时刻,我会这样删除它:
deleteCookie("MyCookie");
Run Code Online (Sandbox Code Playgroud)
只是,这不起作用; cookie仍然存在.那么,有谁知道为什么?