我试图AJAX JSON数组,但由于某种原因,当我使用JSON。Uncaught SyntaxError: Unexpected token
这是我的PHP:
$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);
这里是Javascript:
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();
下面就是PHP要回显的内容:
{"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"}]}
如果不使用JSON。解析和console.log响应,我收到它作为一个字符串。
除了字符串开头的无效不可见字符''ufeff'
之外,您的输出解析得很好。这是UTF-16字节顺序标记。
Content-Type
标头中发送不正确/缺失charset
的字符串。