我已经完成了关于这个主题的所有其他问题,但我似乎无法找到解决这个相对容易的问题的方法:
console.log(xmlhtpp.responseText)导致:
[{"id":"1","name":"Filosofie","image":"yin-yang.png","background_color":"no"},{"id":"2","name":"Politiek","image":"politics.png","background_color":"no"},{"id":"3","name":"Geschiedenis","image":"history.png","background_color":"no"},{"id":"4","name":"Vocabulaire","image":"vocabulary.png","background_color":"no"},{"id":"5","name":"Wetenschap","image":"science.png","background_color":"no"}]
当我尝试将字符串解析为如下对象时出现问题:
JSON.parse(xmlhttp.responseText);
导致以下错误:
未捕获的SyntaxError:意外的输入结束
该字符串源自PHP文件:
$results = $db->query("SELECT * FROM library ORDER BY id", true);
$categories = array();
while ($row = mysqli_fetch_assoc($results)) {
$categories[] = $row;
}
echo json_encode($categories);
我需要最终循环通过对象,但我无法通过解析,任何建议都会非常感激。
答案 0 :(得分:1)
XAMPP的默认Content-Type是text / html,所以你的浏览器试图像这样解析..
自己设置内容类型..
header('Content-Type: text/javascript');
echo json_encode(["foo", "bar"]);