2

下面给出的 URL 中的 JSON 数据如下所示:

{
   "datum":[
      {
         "id":"19",
         "song":"The Age of Worry",
         "path":"audio/age.mp3",
         "artist":"John Mayer",
         "album":"Born And Raised",
         "genre":"Blues-Rock",
         "year":"2012",
         "filename":"age",
         "extension":".mp3",
         "size":"6.1539077758789 MB ",
         "duration":"2:40",
         "downloads":"0"
      }
   ]
}

我想提醒歌曲名称。下面的代码只提醒 id 而不是歌曲。我无法弄清楚这里出了什么问题。


switch(id){
        case "1":
            alert(id);              
            $.getJSON("http://localhost/musicstore/search.php?media=audio&search_by=song&search_string=age", function(d) {
                 $.each(d.datum, function (i, res) {
                       alert(res.song);                     
                    });

            }); 

            break;
        case 2:
            break;
        case 3:
            break;
        }
4

3 回答 3

4

我添加了这个小提琴。假设您的 JSON 数据像您的示例所说的那样返回,您的 .each 逻辑工作得很好。所以问题一定出在您返回 JSON 的 PHP 脚本中。

这是简化的小提琴,仅用于测试您的 .each 逻辑:

var jsonData = {"datum":[{"id":"19", "song":"The Age of Worry", "path":"audio/age.mp3", "artist":"John Mayer", "album":"Born And Raised", "genre":"Blues-Rock", "year":"2012", "filename":"age", "extension":".mp3", "size":"6.1539077758789 MB ", "duration":"2:40", "downloads":"0" }]};
$.each(jsonData.datum, function (i, res) {
    alert(res.song);                     
});​
于 2012-11-12T10:27:52.753 回答
0

假设响应有效,验证来自服务器的响应的 Content-Type 标头是否正确设置为 application/json

于 2012-11-12T10:24:58.093 回答
0

据我所知,当你想提醒 json 数据时,它只显示[object Object],使用 console.log() 代替,需要为此安装 firebug

于 2012-11-12T10:22:07.670 回答