0

I am creating cookies using jQuery cookie, which are setting when I don't use JSON.stringify as in the following:

$.cookie("previousObject", savedObjs);

but which do not set when I use JSON.stringify(), as in the following:

$.cookie("previousStories", JSON.stringify(savedObjs));

I have also tried the following:

$.cookie.json = true;
$.cookie("previousObject", savedObjs);

The cookie that should be being created logs to the console,but when I look under resources in the browser, there are no cookies there.

How do I make these cookies work?

3
  • Is there an error in the console? Commented May 15, 2015 at 19:05
  • @epascarello no errors...it logs with "previousStories= plus the stringified object Commented May 15, 2015 at 19:06
  • There is nothing wrong with your usage. Make sure you open the Cookies section under resources, it will be tucked under the host name of your site. Commented May 15, 2015 at 19:10

1 Answer 1

1

You don't need to explicitly stringify an object before passing it to $.cookie() since the latter does this for you automatically anyway.

Here's the part of its code that encodes your value:

function stringifyCookieValue(value) {
    return encode(config.json ? JSON.stringify(value) : String(value));
}

So your last bit of code is actually right:

$.cookie.json = true;
$.cookie("previousObject", savedObjs);

In Firefox you can find your cookies in Storage Inspector: https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector#Cookies

Chrome shows them in Resources: https://developer.chrome.com/devtools/docs/resource-panel#cookies

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

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.