SyntaxError: JSON.parse: unterminated string #143
Comments
It looks as if the value of the cookie starts with a Could you look, in the debugger, at the value right before it gets written? Or am I overlooking something? And then I'm wondering anyway whether json cookies would need the un-RFC-2068 treatment ever. |
Are you writing said cookie on the server? |
The only way I could trigger this is if I wrap the entire json in quotes, which were then no a json in the first place, such as: $.cookie.json = true;
$.cookie('links', '{"links": [{"name": "AOL","url": "www.aol.com"}, {"name": "Yahoo!","url": "www.yahoo.com"}, {"name": "Microsoft","url": "www.microsoft.com"}, {"name": "CNN","url": "www.cnn.com"}, {"name": "Apple","url": "www.apple.com"}, {"name": "Digg","url": "www.digg.com"}, {"name": "Amazon","url": "www.amazon.com"}]}')
$.cookie('links') // throws SyntaxError: JSON.parse: unterminated string You would need to remove those quotes if you're using it like this. |
I am closing this, I would need more information to proceed with this (and eventually reopen) but it seems to me it's incorrect usage. |
Thank you so much for your replies. I updated to your newest build and the error message I'm getting now is different, but I have a feeling it's still related. I created a very simplified version of the page that has the json as a string in a variable and puts it int the cookie with no issues. When it tries to read it into the "test" variable it gets the following error... JSON.parse: unexpected non-whitespace character after JSON data It seems it chokes at... cookie = JSON.parse(cookie); Here's an example page - http://pomona.edu/carhartl/ |
It's as I suspected. In that example you're using a string: jsonObj = '{"links": [...]}' // That's a string If you want to use the plugin's build-in JSON support, you would pass an object right away as value: jsonObj = {"links": [...]} // That's an object that get's serialized automatically by the plugin |
I fixed my example to use JSON.parse to feed it the object version of the string, which did seem to fix that part of the issue (THANK YOU)...but I'm noticing that if i test to see if a cookie exists or not (and it doesn't exist) I get that same error. I modified my example. It should have two alerts, one for the first cookie it finds and one for the second cookie it doesn't find. Instead, when it hits tries to check the second (non-existing) cookie I get the same error of... JSON.parse: unexpected non-whitespace character after JSON data ...that again seems to choke at... cookie = JSON.parse(cookie); Here's the example page - http://pomona.edu/carhartl/ |
I am still not convinced. If we were silently eating that exception imo it would have taken much longer to get to the root cause. |
And now ppl can't use json = true with other cookies who are not in json format. Let's take the jsessionid which you should pass on every request if your cookies are disabled or store in the cookies otherwise. If you still want to throw the exception we should detect a json format and ignore the json parse, don't you thinl it will add too much code to the codebase? |
I was thinking that one had to reset the json config when dealing with different type of cookies. That probably turns out as impractical. Then let's go the route eating the exception... :) The thing is, we hardly need the json config flag at all in the first place. |
But how does catching the exception help with the case of trying to read a regular cookie while |
I just wanted to make sure I'm understanding things properly. When I first listed this issue I was able to check to see if a cookie existed or not by something like the following. if ($.cookie('doesnt_exist') == 'null') That worked until I updated my jquery.cookie.js code because I saw there had been some updates. Now that same code errors. The documentation says that checking for a cookie that doesn't exist should result in "null" but that no longer seems to happen because of code changes in the last few days. Sorry if I'm misunderstanding anything. |
Not quite right. You can and always could test wether a cookie exists or not - as can be verified by the tests like so: if ( $.cookie('doesnt_exist') === null ) // NOTE null *not* 'null' Or simply if ( !$.cookie('doesnt_exist') ) Fwiw, in the new release there's now yet another way to do this, which might read better even: var cookies = $.cookie();
if ( 'foo' in cookies ) // If true we know that the cookie named "foo" exists... The problems you were facing were, as far as I could see, all related to wrong usage of json cookies, in that you tried to pass a string with Last not least, what we're now trying to figure out is what's the best way to deal with these kind of errors, or invalid JSON in general and whether HTH |
My apologies, in fact you're right, this now will also throw an error: $.cookie.json = true;
$.cookie('doesnt_exist') // => SyntaxError: JSON Parse error: Unexpected identifier "bar" I will come up with a fix and a 1.3.1 release soon. |
You will need to check if the cookie is a json first. See those huge regex from https://github.com/douglascrockford/JSON-js/blob/master/json2.js. Thats what I am talking about. |
@FagnerMartinsBrack Yeah I agree, that's beyond scope of the plugin. |
1.3.1 released. How in general we want to handle invalid JSON is discussed in #132... |
I apologize in advance if I'm missing something obvious, but I have some pretty simple json...
{"links": [{"name": "AOL","url": "www.aol.com"}, {"name": "Yahoo!","url": "www.yahoo.com"}, {"name": "Microsoft","url": "www.microsoft.com"}, {"name": "CNN","url": "www.cnn.com"}, {"name": "Apple","url": "www.apple.com"}, {"name": "Digg","url": "www.digg.com"}, {"name": "Amazon","url": "www.amazon.com"}]}
...which I'm storing in a cookie.
I'm using "$.cookie.json = true;" which works fine, but when I try to read it back (Firefox 18 - Mac) I get the message...
SyntaxError: JSON.parse: unterminated string.
I used firebug to trace it and when it gets to the line...
value = value.slice(1, -1).replace('"', '"').replace('\', '');
The cookie variable result is...
"{"links\": [{\"name\": \"AOL\",\"url\": \"www.aol.com\"}, {\"name\": \"Microsoft\",\"url\": \"www.microsoft.com\"}, {\"name\": \"CNN\",\"url\": \"www.cnn.com\"}, {\"name\": \"Yahoo!\",\"url\": \"www.yahoo.com\"}, {\"name\": \"Apple\",\"url\": \"www.apple.com\"}, {\"name\": \"Digg\",\"url\": \"www.digg.com\"}, {\"name\": \"Amazon\",\"url\": \"www.amazon.com\"}]}"
If I replace all the "\" with "" it works fine. I've double checked that my json validates, so I'm thinking it must be an issue with the escaping replace statement (only the first one seems to be escaped properly). If there's anything else I can provide, please let me know. For now, I've found that using the "raw" option sidesteps this issue. Thanks so much for all of your work on this plugin. It's a huge help.
The text was updated successfully, but these errors were encountered: