I am setting cookie server-side, using java code
response.addCookie("test1","test1");
I found this code to retrieve cookie using javascript
(function(){
var cookies;
function readCookie(name,c,C,i){
if(cookies){ alert("all cookies"+cookies); return cookies[name]; }
c = document.cookie.split('; ');
cookies = {};
for(i=c.length-1; i>=0; i--){
C = c[i].split('=');
cookies[C[0]] = C[1];
}
alert("required cookie"+cookies[name]);
return cookies[name];
}
window.readCookie = readCookie; // or expose it however you want
})();
I am calling this function as
alert(readCookie('test1'));
but every time I get the alert as undefined.. I checked the chrome's cookie file and my cookie is set there as
localhosttest1test1/service/login
Can someone explain why am I getting this error?