I am using Rest response to set cookies on the client side. But I cannot see the cookie being set when I open up 'Resources' in Chrome. But interestingly, when I go to chrome settings and check all cookies, I find the cookies I am setting. Again, getCookie() javascript function from w3schools (or better version to handle all possibilities) fetch me nothing. I tried firefox, there same thing happens. When I see all cookies, I see my cookies, but JS function getCookie() does not return me anything. I think the cookies are not getting set properly.
Here is my JAX-RS response :
Cookie c1 = new Cookie(Constants.SESSION_TOKEN, response .getSessionToken().getValue()); Cookie c2 = new Cookie(Constants.USER_IDENTIFIER, response.getUserIdentifier()); NewCookie cookie1 = new NewCookie(c1); NewCookie cookie2 = new NewCookie(c2); return Response.ok(jsonResponse, MediaType.APPLICATION_JSON) .cookie(cookie1,cookie2).build();
And this is my JS getCookie() function
function getCookies() { var c = document.cookie, v = 0, cookies = {}; if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) { c = RegExp.$1; v = 1; } if (v === 0) { c .split(/[,;]/) .map( function(cookie) { var parts = cookie.split(/=/, 2), name = decodeURIComponent(parts[0] .trimLeft()), value = parts.length > 1 ? decodeURIComponent(parts[1] .trimRight()) : null; cookies[name] = value; }); } else { c .match( /(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g) .map( function($0, $1) { var name = $0, value = $1.charAt(0) === '"' ? $1 .substr(1, -1).replace(/\\(.)/g, "$1") : $1; cookies[name] = value; }); } return cookies; } function getCookie(name) { return getCookies()[name]; }