2016-04-26 96 views
-1

我想顯示網頁上播放的最後10首歌曲。這裏是我正在使用的代碼,但我無法從json文件中檢索和顯示請求的信息。 https://jsfiddle.net/Heropiggy95/6qkv7z3b/有人可以指出錯誤或展示一個工作示例嗎?謝謝。需要幫助解析json網頁上的數據

$(document).ready(function() { 
    $.getJSON("http://apps.radioactive.sg/lastsongsplayed.php?stationId=995fm&callback=myfunction", function(data) { 
    var html = ''; // declare the variable that will be used to store the information 
    $.each(data.myfunction, function(i, item) { 
     { 
     html += 'Song: ' + item.song.track + ', Artist: ' + item.song.artist + ', Time: ' + item.playedtime + ''; 
     } // 
    }); // 
    $('.nowplaying').append(html); // print the information to the document with a span class of 'nowplaying' and use the jQuery append method to insert the information. 
    }); // close JSON call 
}); // close document ready function 
+0

'它不是working'可能是你能提供的最糟糕的信息。請給一些更多的細節。 – C4u

回答

0

嘗試使用JSON.parse這樣

$(document).ready(function() { 
    $.getJSON("http://apps.radioactive.sg/lastsongsplayed.php?stationId=995fm", function(data) { 
    var html = ''; // declare the variable that will be used to store the information 
    var parsedData = JSON.parse(data); 

    $.each(parsedData, function(i, item) { 
     { 
     html += 'Song: ' + item.song.track + ', Artist: ' + item.song.artist + ', Time: ' + item.playedtime + ''; 
     } // 
    }); // 
    $('.nowplaying').append(html); // print the information to the document with a span class of 'nowplaying' and use the jQuery append method to insert the information. 
    }); // close JSON call 
}); // close document ready function 
+0

這是我一直在努力的代碼,但我似乎無法生成輸出。 https://jsfiddle.net/Heropiggy95/6qkv7z3b/我不知道我應該採取什麼措施來糾正問題。你能告訴我如何解決這個頁面?謝謝。 – HeroPiggy95

+0

您是否嘗試在localhost或任何其他域上運行此代碼。,我試圖在本地運行它並給出: 否請求的資源上存在「Access-Control-Allow-Origin」標頭。 Origin'http://fiddle.jshell.net'因此不被允許訪問。 您需要在您的服務器上處理此問題,以允許您的域發送ajax請求。 –

+0

我想在本地主機上運行它。我做了我的研究,這表明'Access-Control-Allow-Origin'可以通過在jsonp中使用ajax來繞過。 http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp是否有可能使用此方法從服務器獲取信息? – HeroPiggy95