Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookie written in JavaScript not being read in code-behind

Response.Cookies["alertsCookie"] gives me a blank cookie.

I have made two cookies since I could not find out how to read a cookie in a path, so I decided to write them to both locations (the page path and /)

Code-behind:

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);
}
like image 341
user1690294 Avatar asked Oct 19 '12 20:10

user1690294


People also ask

Can JavaScript read a cookie?

JavaScript can create, read, and delete cookies with the document.cookie property. With JavaScript, a cookie can be created like this: document.cookie = "username=John Doe"; You can also add an expiry date (in UTC time).

Can JavaScript manipulate cookies?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.


1 Answers

Look in Request instead of Response.

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

Response.Cookies is for setting cookies on the browser, Request.Cookies is for reading cookies coming from the browser.

like image 150
Chuck Conway Avatar answered Sep 30 '22 19:09

Chuck Conway