My attempt to AJAX a JSON array is hitting a snag - when I utilize JSON.parse, an error pops up: Uncaught SyntaxError: Unexpected token
Take a look at my PHP snippet:
$infoJson = array('info' => array());
while($row = mysqli_fetch_array($query))
{
array_push($infoJson['info'],
[
'section' => $row['section'],
'source' => $row['source'],
'project' => $row['project'],
'client' => $row['client'],
'date' => $row['date'],
'id' => $row['id']
]);
}
echo json_encode($infoJson);
Now check out the Javascript code:
request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if(request.readyState == 4 && request.status == 200)
{
var response = request.responseText;
response = JSON.parse(response);
}
}
request.open('GET','http:edit.php?requestedArray=printArray',true);
request.send();
Here's what the PHP prints out:
{"info":[{"section":"printArray","source":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/7\/7a\/SNES-Controller.jpg\/1280px-SNES-Controller.jpg","project":"SNES","client":"Nintendo","date":"1990","id":"7"},{"section":"printArray","source":"http:\/\/ecx.images-amazon.com\/images\/I\/81Q0l1t%2BaJL._SL1500_.jpg","project":"Playstation","client":"Sony","date":"1994","id":"8"},{"section":"printArray","source":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/ed\/Xbox-360-S-Controller.png","project":"Xbox 360","client":"Microsoft","date":"2005","id":"9"}]}
If I skip using JSON.parse and simply log the response, it does come through as a string.