【发布时间】:2017-05-08 20:26:51
【问题描述】:
我有一个 php 文件,它创建一个 json 并将其传递给 javascript
<?php
$index = 0;
$connection = mysql_connect(*secret*,*secret*,*secret*);
mysql_select_db("sql7150348");
$query =mysql_query("SELECT * FROM statements");
while ($row = mysql_fetch_assoc($query)) {
$array[$index] = $row;
$index++;
}
echo json_encode($array);
然后我用 $.post 接收这些数据
[{
"Titel": "Begroting",
"Text": "In crisistijden mag de Vlaamse begroting in het rood gaan",
"Voor": "spa",
"Tegen": "vld",
"PuntVoor": 0,
"PuntTegen": 0
}]
$.post('../php/getFromDatabase.php', function () {}).done(function (data) {
console.log(JSON.parse(data));
});
但我总是收到这个错误(关于console.log(JSON.parse(data));)
未捕获的 SyntaxError:JSON 输入意外结束
在 JSON.parse()
在 Object.success (script.js:20)
在我 (jquery.min.js:2)
在 Object.fireWith [as resolveWith] (jquery.min.js:2)
在 A (jquery.min.js:4)
在 XMLHttpRequest。 (jquery.min.js:4)
我做错了什么来得到这个错误?
【问题讨论】:
-
你能发布你的 JSON 字符串吗?
-
你的浏览器控制台是否收到来自服务器的 json 响应?
-
json 已经在帖子里了,
-
如何检查 json 响应?
-
你得到的是有效的 json,所以不需要
parse它,在你的逻辑中直接尝试data;
标签: php json jquery-post