I have a Sencha Touch app. One of the stores I have uses an ajax proxy and a json reader. Some of the strings in the JSON returned from my sinatra app occasionally contain this character: http://www.fileformat.info/info/unicode/char/2028/index.htm
Although it's invisible, the character occurs twice in the second string here, between the period and the ending quote:
"description": "Each of the levels requires logic, skill, and brute force to crush the enemy.
"
Try copy and pasting "Each of the levels requires logic, skill, and brute force to crush the enemy.
" into your javascript console! It won't be parsed as a string, and fails with SyntaxError: Unexpected token ILLEGAL
.
This causes the JSON response to fail. I've been stuck on this for a long time! Any suggestions?
.replace()
filters on the string before you eval()
it - Travis Webb 2012-04-05 00:34
The only reliable way to fix this is server-side. Make sure your JSON generator emits those characters escaped, e.g. as \u2028
.
In my experience, it's easiest to simply encode your JSON in plain ASCII which will always work. The downside is that it's less efficient as non-ASCII characters will take up more space, so depending on the frequency of those, you may not want that trade-off...
The documentation for Perl's JSON::XS has a good explanation of the problem and advice for how to fix it in Perl: http://search.cpan.org/perldoc?JSON::XS#JSON_and_ECMAscript
Conceptually you are only allowed to send out strings from the server that are valid JavaScript literals by escaping appropriately.
If you want to fix this issue on the client you need an extra workaround step (only seems to work in Firefox):
var a = escape("Each of the levels requires logic, skill, and brute force to crush the enemy.");
alert(unescape(a));
But the discussion is obsolete, because you must escape on the server.
:
and pasted it in console, got syntax error - Salman A 2012-04-17 05:36
Avoid using eval
to parse JSON.
Use JSON.parse
or https://github.com/douglascrockford/JSON-js.
JSON.parse('{}'); // where {} is your JSON String