0

So I'm new to jQuery and JSON but this is pretty straight forward and the fact the it isn't working is frustrating me.

I have an ASP.NET MVC Web site. I'm using AJAX (through jQuery) to load some data. Everything is well and good up until there.

I'm trying to store my result in a cookie by using carhartl / jquery-cookie.

The problem is when I try to store my data in a cookie the data isn't really stored. I'm using the code below:

                var jsonObj = JSON.stringify(result);

                jQuery.cookie("resultData", jsonObj, {
                    expires: 2,
                    path: "/",
                    json: true
                });

jQuery.cookie("resultData") returns null

I've tried running the same code but instead of my actual data I wrote "hello word" which worked just fine.

Can anyone help me please ?

6
  • What's the size of your json ? You have a limit of 4K for each cookie, I think :) Commented Nov 8, 2012 at 16:24
  • 7808 characters, is it the problem ? Commented Nov 8, 2012 at 16:25
  • @Johny, see stackoverflow.com/questions/640938/… The overall length of the cookie should not exceed 4096 bytes.. Commented Nov 8, 2012 at 16:27
  • As I said: 4KB. Try with a smaller json. Like { test : 1 } and see the result Commented Nov 8, 2012 at 16:28
  • @Mihai and [at]Gaby aka G. Petrioli yep that's the problem ... lame. Thanks though if [at]Mihai puts this as an answer I can mark it as the correct answer :) Commented Nov 8, 2012 at 16:36

2 Answers 2

2

The max size of a cookie is 4 KB. Check that :) Try with a smaller json, like { test : 1 } and see if that works

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

Comments

0

I guess the issue is that json: true is not an option, but it's the property of jQuery.cookie object itself.

So you should execute this line somewhere before your code:

jQuery.cookie.json = true;

and after that execute your code.

So, it could be something like that:

jQuery.cookie.json = true;

jQuery.cookie("resultData", jsonObj, {
    expires: 2,
    path: "/"
});

And note that, if you set $.cookie.json = true, you don't need to json-stringify your object - it would be provided automatically.

Please refer to official doc or answer on similar question here.

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.