用JavaScript编写的Cookie没有在代码隐藏中读取

时间:2012-10-19 20:01:38

标签: c# javascript jquery asp.net cookies

Response.Cookies["alertsCookie"]给了我一个空白的cookie。

我已经制作了两个cookie,因为我无法找到如何在路径中读取cookie,因此我决定将它们写入两个位置(页面路径和/)

代码隐藏:

HttpCookie seenAlertsCookie = Response.Cookies["alertsCookie"];

JavaScript(jQuery):

var cookie = $.cookie("alertsCookie");
alert(cookie);
if (cookie == null) {
    $.cookie('alertsCookie', alertGuid, { expires: 7300, path: '/' });
    $.cookie('alertsCookie', alertGuid, 7300);

}
else {
    var cookieVal = cookie + '|';
    cookieVal = cookieVal + alertGuid;
    $.cookie('alertsCookie', cookieVal, { expires: 7300, path: '/' });
    $.cookie('alertsCookie', cookieVal, 7300);
}

1 个答案:

答案 0 :(得分:15)

查看Request而不是Response

HttpCookie seenAlertsCookie = Request.Cookies["alertsCookie"];

Response.Cookies用于在浏览器上设置Cookie,Request.Cookies用于读取来自浏览器的Cookie。