【问题标题】:Read JSON returned by PHP in Javascript用 Javascript 读取 PHP 返回的 JSON
【发布时间】:2016-02-26 14:47:43
【问题描述】:

我想从数据库中选择一些内容并将其返回给 javascript。数据库返回了几行。我用 JSON 进行了尝试,如果我打印出来,也得到了结果。但是如果我想转换 JSON 字符串,总是有下面的错误信息。 (在 JSON.parse 中)所以,我假设在填充数组时可能会出错?提前谢谢各位!

Javascript:

$.ajax({
  url: "./select_firsttracks.php",
  type: "post",
  success: function(resultset) {
    $("#erroroutput").html(resultset);

    var arr = JSON.parse("{" + resultset + "}"); // --> "Uncaught SyntaxError: Unexpected token {"            
  },
  error: function(output) {
    $("#erroroutput").html("fatal error while fetching tracks from db: " + output);
  }
});

PHP:

$storage = array();
while($row = $result->fetch_array())
{
    $storage[] = 
        array
        (
            "id" => $row["id"],
            "trackname" => $row["trackname"],
            "artist" => $row["artist"],
            "genre" => $row["genre"],
            "url" => $row["url"],
            "musicovideo" => $row["musicovideo"]
        );

    echo json_encode($storage);
}

控制台输出:

[{"id":"1","trackname":"yes","artist":"Lady Gaga","genre":"Pop","url":"ftp:\/development","musicovideo":"1"}][{"id":"1","trackname":"yes","artist":"Lady Gaga","genre":"Pop","url":"ftp:\/development","musicovideo":"1"},{"id":"2","trackname":"no","artist":"Prinz Pi","genre":"Rap","url":"ftp:\/development","musicovideo":"1"}]

【问题讨论】:

  • 您的结果集已经是有效的 JSON;添加大括号会变得很糟糕。
  • 不需要在JSON.parse("{" + resultset + "}"); 中连接{} 代码: JSON.parse(resultset); 或在ajax 配置中添加dataType: 'json',

标签: javascript php jquery arrays json


【解决方案1】:

在一段时间后回显 json

$storage = array();
while($row = $result->fetch_array())
{
    $storage[] = 
        array
        (
            "id" => $row["id"],
            "trackname" => $row["trackname"],
            "artist" => $row["artist"],
            "genre" => $row["genre"],
            "url" => $row["url"],
            "musicovideo" => $row["musicovideo"]
        );


}
echo json_encode($storage);

并改变:

 var arr = JSON.parse(resultset);

【讨论】:

    【解决方案2】:

    您在收到的 JSON 的前后添加花括号,这里:

    var arr = JSON.parse("{" + resultset + "}");
    

    Phps json_encode 自身返回完全有效的 JSON。不加括号试试看:

    var arr = JSON.parse(resultset);
    

    【讨论】:

      【解决方案3】:

      生成的json字符串无效,可以用jsonlint检查

      在循环外修改你的php代码为echo

      while($row = $result->fetch_array())
      {
          $storage[] = 
              array
              (
                  "id" => $row["id"],
                  "trackname" => $row["trackname"],
                  "artist" => $row["artist"],
                  "genre" => $row["genre"],
                  "url" => $row["url"],
                  "musicovideo" => $row["musicovideo"]
              );
      
      
      }
      echo json_encode($storage);
      

      而在 javascript 中,只需将输出用作 javascript 对象

      success: function(resultset) {
          console.log(resultset)
          resultset.each(function(index,element){ console.log(index,element )})
        },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多