$.ajax is giving me a parse error about some JSON returned by my PHP script. I’m somewhat manually encoding JSON with this function extending my PDO statement, in the hopes of saving some memory for very large datasets.
Why won’t this work? If I copy/paste the response text into JSON.parse in my editor, it works fine. So it’s likely some escaping issue?
Also, if I take out the top three lines (encoding x, y, and rows) and just return it an array, it works fine. Just not as my pieced together object.
public function toJSON ($x, $y) {
echo "{x:" . json_encode($x) . ",";
echo "y:" . json_encode($y) . ",";
echo "rows:[";
$i = 0;
while ($row = $this->fetch(PDO::FETCH_ASSOC)) {
if ($i) echo ",";
echo json_encode($row, JSON_NUMERIC_CHECK);
if ($i > 0 && $i % 100 === 0) {
ob_flush();
flush();
}
$i++;
}
echo "]}\n";
}
Are you sure you’re putting it in the correct format?
Your name needs to be in quotations because it’s a string. If your value is a string it also needs to be in quotations.
ex/
echo "{x:" . json_encode($x) . ",";needs to be like so:
echo '{"x":' . json_encode($x) . ',';