Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad control character error in json parse

When parsing a JSON object I am getting "bad control character" error in the Firebug console. There are lot of questions and solutions in this site; but I am unable to crack this issue. I believe I am doing something silly. Please point me to my mistake.

JS Fiddle: http://jsfiddle.net/Purus/Eqz2r/

If we change the json data to a plain text instead of html tags, it works.

Error:

SyntaxError: JSON.parse: bad control character in string literal
someVal = JSON.parse(sign);

NOTE:

  • The JSON data is obtained from a PHP function using json_encode function.
  • The json output looks valid in jsonlint

Below is the code I have used.

$(document).ready(function () {
   var sign = '{"data":"<br\/><br\/>----------<br \/>\r\nFrom Yahoo Team<br \/>\r\n<a href=\"http:\/\/localhost\/base\/1-yahoo-logo.jpg\" target=\"_blank\"><img style=\"padding: 5px;\" src=\"http:\/\/localhost\/plugins\/1-yahoo-logo.jpg\" height=\"120\" width=\"196\" \/><\/a>"}';

   someVal = JSON.parse(sign);
   $(".demo").append(someVal.data);
});
like image 698
Purus Avatar asked Mar 08 '26 20:03

Purus


1 Answers

You need to escape your escapes :)

Use double \\ instead of \

http://jsfiddle.net/Eqz2r/2/

like image 117
TimCodes.NET Avatar answered Mar 11 '26 10:03

TimCodes.NET



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!