JSON.parse Crash in Safari when string value contains comma

I create a shopping cart that, when I click the Buy button, the web service returns JSON output, which I then save as a Javascript file as a string.

A typical return from a web service might be:

{ "d":{ "58658":{ "id":"58658", "qty":"1", "singlePrice":"754", "salePrice":"754", "isBulk":"0", "isSor":"0", "vatRate":"20.00", "masterCat":"6", "imgUrl":"http://...img url", "singleWeight":"18000", "totalAvailableStock":"2", "thirdPartyStock":"", "item":"Electrovoice Live X Elx115p, Each " // << HERE IS THE ISSUE } } } 

When the return looks like the one shown above, it will only work in Safari

After spending a lot of time trying to find some useful mechanism for debugging this without buying a Mac, I finally traced it to:

 "item":"Electrovoice Live X Elx115p, Each " // << HERE IS THE ISSUE - The comma 

The item value has a comma in the string, I save the return value to a cookie with something like:

 cookies.set('shopping_cart', JSON.stringify(result)); // (where result is the JSON above) 

When I try to recall this later:

 var shopping_cart = cookies.get('shopping_cart'); shopping_cart = JSON.parse(shopping_cart); 

It works fine in any browser, even, apparently, in Internet Explorer ... EXCEPT Safari

I don’t have a Mac, so I'm not sure if Safari on Mac is different than others, but of course the Windows version has this error and this happens on my iPhone and iPad.

Error:

 JSON parse error, unterminated string. 

As I said, this only seems to be a problem in Safari, but I find it difficult to find a solution.

Help rate!

EDIT:

I created a script: http://jsfiddle.net/jhartnoll/2GLEz/ This script will replicate the problem, it is associated with storing data as a string in a cookie and reanalyzing it in JSON

But since I turned on my Cookie features as an external resource ( http://www.sam.thinkka.com/clientscripts/cookie_test.js ), you will need to make sure that Safari does not block third-party cookies.

+7
source share
3 answers

Ok, now we have the root of the real problem, I think I can help.

The problem is that you are storing data in a cookie string, but do not avoid it. Therefore, when you set a cookie and contains commas, commas can be considered cookie delimiters. For most of your string, quotes in JSON seem to mask this effect (quotes are also important characters for cookies), but for this it is considered by the cookie parser as a separator.

This, in turn, means that it is regarded as the end of the cookie, which means that when it is loaded, it is truncated at this point.

Solution: Avoid the line before saving it as a cookie. (your makeCookie() function should do this). You must use JS escape() to do this.

See the cookie specification and note that commas, double quotes, half columns, etc. are important symbols for cookies.

See also: this similar problem

As for why this happens in Safari and not in other browsers ... I guess it's probably good luck, like everything else. Perhaps the cookie analysts of other browsers recognize that the next line after the decimal point is invalid and therefore works, that the comma is not intended as a separator.

You can also see the MDN cookie article , which includes code for a sample Jook coookie library that does all the proper escaping. You may want to use this lib instead of your own current code. (or at least parts of it)

Hope this helps.

Finally, a little off topic, but one more thing to be aware of when using cookies: do not forget that the entire cookie string is sent in both directions for each individual HTTP request made on your site. So if you have 4k cookie data, this means that 8k bandwidth will be added to your html, css, js and image file on your website.

If you use large cookies, there are alternative ways of storing local data that are not prone to this problem. This article may be helpful to you.

+9
source

you do not need to parse this JSON ... try:

 var a= { "d":{ "58658":{ "id":"58658", "qty":"1", "singlePrice":"754", "salePrice":"754", "isBulk":"0", "isSor":"0", "vatRate":"20.00", "masterCat":"6", "imgUrl":"http://...img url", "singleWeight":"18000", "totalAvailableStock":"2", "thirdPartyStock":"", "item":"Electrovoice Live X Elx115p, Each " // << HERE IS THE ISSUE } } } console.log(ad['58658']) 
0
source

the text must be at least 40 characters, years 11

0
source

Source: https://habr.com/ru/post/955908/


All Articles