簡體   English   中英

未捕獲的類型錯誤:undefiened不是JSON響應的函數

[英]uncaught type error :undefiened is not a function for JSON response

嗨我正在使用json將一個歌曲列表發送到我的桌子.Json響應很好,直到控制器url.But當我嘗試迭代它並將詳細信息放在我的表中時,我收到此錯誤。

  $(window).load(function(){
        $.get("/MusicPlayer/getSongList", function(data){
         var library = data;
         listSongs(library);
    });
}); 


 function listSongs(library){
 var table = $("#table");
 var row;
 $("#table tbody").remove();
 library.forEach(function(song){ ***/*this is the line where there is error. */*** 
     row = $("<tr></tr>");
        $("<td />").addClass("song-select").append($("<input  
        />").attr({type:"checkbox",class:"checkbox",value:song.title})).appendTo(row);
        $("<td>"+song.title+"</td>").appendTo(row);
        $("<td>"+song.album+"</td>").appendTo(row);
        $("<td>"+song.artist+"</td>").appendTo(row);
        $("<td>"+song.rating+"</td>").appendTo(row);
        $("<td>"+song.composer+"</td>").appendTo(row);
        $("<td>"+song.genre+"</td>").appendTo(row);
        row.click(viewFunction());
        row.appendTo(table);
 });


 [
{
    "title": "15 - Jacob's Theme.mp3",
    "album": "The Twilight Saga - Eclipse",
    "artist": "Howard Shore",
    "rating": "2",
    "composer": "carter ruwell",
    "genre": "Soundtrack"
},
{
    "title": "07_-_Vennante.mp3",
    "album": "Andala Rakshasi (2012)",
    "artist": "Ranjith",
    "rating": "0",
    "composer": "Rathan",
    "genre": "TeluguMusic"
},
{
    "title": "08. One Simple Idea.mp3",
    "album": "Inception (OST)",
    "artist": "Hans Zimmer",
    "rating": "0",
    "composer": "null",
    "genre": "?????????"
}
   ]

我猜你需要$.parseJSON ,因為library應該是一個JSON格式的字符串,而不是一個真正的數組。

$.parseJSON(library).forEach(function(song){ .....

一個json響應是在一個非常特殊格式的字符串 ,但仍然只是一個字符串。

在能夠將其用作Javascript數據之前,您需要使用JSON.parse解析它。

json對象沒有為它定義forEach函數。

用於循環:

for(var song in library) {
   console.log(song, result[song]);
}

或者each jquery:

$.each(library, function(key, value) {

});​

forEach僅適用於簡單數組和支持ECMAScript5的環境。 看看這個: For-each在JavaScript中的數組?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM