Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing PHP response: Uncaught SyntaxError: Unexpected token <

I'm using AJAX to make a call to a PHP script. The only thing I need to parse from the response is a random ID generated by the script. The problem is that the PHP script throws a number of errors. The errors are actually fine and don't get in the way of the program functionality. The only issue is that when I run

$.parseJSON(response)

I get:

Uncaught SyntaxError: Unexpected token < 

Since the PHP response starts with an error:

<br /> 
<b>Warning</b>:

I'm wondering how to change the PHP or JS such that it can parse out the ID despite the errors.

PHP:

  $returnData = array();
  $returnData['id'] = $pdfID;
  echo json_encode($returnData); 
  ...

JS:

 function returnReport(response) {
    var parsedResponse = $.parseJSON(response);
    console.log(parsedResponse);
    pdfID = parsedResponse['id']; 

I know that the warnings should be resolved, but the warnings are not functionality critical for now and more importantly

1) Even if these warnings are resolved new ones may come up down the line and the JSON should still be properly parsed and

2) In addition to the warnings there are 'notices' that cause the same issue.

like image 216
Ben Davidow Avatar asked Mar 21 '14 23:03

Ben Davidow


1 Answers

Why not deal with and eliminate the warning so that the result from the server is actually JSON?

like image 77
Kris Oye Avatar answered Nov 15 '22 16:11

Kris Oye