My system runs some MySQL queries in PHP to retrieve data. The data that is retrieved is structured in an array similar to below:
$ret = array(
"name" => "John Doe",
"location" => "Antarctica",
"description" => "Height < 200"
);
I return this through AJAX via:
echo json_encode($ret);
The AJAX error event is fired and upon inspection of the returned data (jqXHR.responseText) via Chrome console, I’m noticing:
"description" : "Height \x3c 200"
which is throwing a parse error.
I know that \x3c is the hex representation of “<” and apparently, that form renders the JSON invalid. I can’t seem to find a way to make it such that I’m able to pass the angled bracket via JSON in a valid form. Direct string replace of “\x3c” on the PHP side doesn’t work.
Further, I know that specifying the AJAX return as HTML will work, but I’m passing a set of data hence the use of JSON.
Try this:
Use
json_last_error()to debug.http://www.php.net/manual/en/function.json-decode.php