0

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?

2
  • Do you get the alerts that are called from readCookie? What do they show? Commented Apr 4, 2013 at 11:32
  • alert("all cookies"+cookies); - shows the alert contents as [object] [object] alert("required cookie"+cookies[name]); - shows nothing except the string message that I gave Commented Apr 4, 2013 at 11:38

2 Answers 2

1

The code seems to work (I tested it in Firefox, Chrome and MSIE, by creating some cookie with JavaScript, e.g., document.cookie = "test1=bla", and it displays "required cookiebla"). Maybe the cookie you set from the server is marked as HttpOnly, so that JavaScript has no access to it?

Sign up to request clarification or add additional context in comments.

6 Comments

so how can I correct that.. I have shown in the beginning of my question that how am I setting the cookie
one thing before trying to look further, could you try an alert(document.cookie) to display all document cookies and see if yours is present?
but this alert,i.e alert("all cookies"+cookies); in the read cookie function showed [object] [object] as output that means it was reading something
Not really, as cookies is an array, converting it to a string to display it in the alert (independently whether it is empty or not) will display [object]. Ok, so the browser cookie array is empty. Now, test if the problem comes from the HTTP cookie, create one locally with JavaScript before calling your function, i.e., with document.cookie = "test1=somestring"; and see if your function displays the "somestring".
both are undefined.. alert(window.cookie); and alert(readCookie('test1'));
|
1

I had a similar problem when I submitted a link that went to a different path. i.e., the page I was clicking the link was /pathOne/somePlace, but the link went to /pathTwo/someOtherPlace. The cookie that was written was only valid on /pathTwo/*. To correct for this I did :

Cookie testCookie = new Cookie("test1","test1");
testCookie.setPath (request.getContextPath ());
response.addCookie (testCookie);

hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.