简体   繁体   English

在Javascript中读取PHP返回的JSON

[英]Read JSON returned by PHP in Javascript

I want to select some content from the database and return it to the javascript. 我想从数据库中选择一些内容并将其返回到javascript。 There are several rows returned by the database. 数据库返回了几行。 I tried this with JSON and also get a result, if I print it out. 如果我打印出来的话,我用JSON尝试了这个并得到了结果。 But if I want to convert the JSON string, there is always the error message below. 但是如果我想转换JSON字符串,则总会出现以下错误消息。 (at the JSON.parse) So, I assume maybe an mistake while filling the array? (在JSON.parse)所以,我假设在填充数组时可能出错了? Thanks in advance guys! 先谢谢你们!

Javascript: 使用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: 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);
}

Output on the console: 控制台上的输出:

[{"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"}]

echo the json after the while 过了一会儿就回应了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);

and change: 并改变:

 var arr = JSON.parse(resultset);

You're adding curly braces in front and behind your received JSON, here: 您在收到的JSON前后添加花括号,在这里:

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

Phps json_encode returns perfectly valid JSON by itself. Phps json_encode返回完全有效的JSON。 Try it without adding the braces: 尝试不添加大括号:

var arr = JSON.parse(resultset);

The resulting json string is not valid, you can check it with jsonlint 生成的json字符串无效,您可以使用jsonlint进行检查

Modify your php code to echo outside the loop: 修改你的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);

And in javascript just use the output as a javascript object 在javascript中只需将输出用作javascript对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM