0

I keep getting an error parsing what seems to be valid JSON. The parse call is done ina handler to an Ajax call to a php script that is returning:

echo json_encode(array("success"=> false, "error_message"=> "error"));

The code bit handling the response:

function(returnedData){
                console.log(returnedData);
                var response = JSON.parse(returnedData);

The console.log call is printing

{"success":false,"error_message":"error"}

If I take this, put it between quotes and manually call JSON.parse in the console, then it works fine.

Any ideas what might be going wrong? Thanks in advance!

  • 1
    Your json isn't json. It's html + json, most likely a php warning/error, causing the json to be considered corrupted, Look at the RAW response from the server to see what this html is. – Marc B Aug 15 '16 at 19:08
  • My guess is that your PHP file is returning HMTL, not your JSON. Check the network tab in your console – Rob M. Aug 15 '16 at 19:08
  • I can't see any way that `console.log(returnedData);` could give output that doesn't include a `<` character when `var response = JSON.parse(returnedData);` throws an error complaining about a `<` character. – Quentin Aug 15 '16 at 19:09
  • It looks as if the data sent by the server has plain text format. Follow [this procedure](http://stackoverflow.com/a/6599300/1505348) to use JSON format instead. And please reply here if it does work :) – Lucio Aug 15 '16 at 19:10
  • That was it! Thanks guys! – Carlos Santos Aug 15 '16 at 19:11

0 Answers0