【问题标题】:Unexpected end of input JSON.parse (PHP, JSON, jQuery.post, database)输入 JSON.parse 意外结束(PHP、JSON、jQuery.post、数据库)
【发布时间】: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


【解决方案1】:

客户端无法识别服务器收到的响应类型。指定响应类型JSON 应该可以修复它。

$.post('../php/getFromDatabase.php', function () {}).done(function (data) {
    console.log(data);
}, "json");

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2021-06-23
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多