-1

I would like to display the last 10 songs played on a web page. Here's the code I'm using but I'm unable to retrieve and display the requested info from the json file. https://jsfiddle.net/Heropiggy95/6qkv7z3b/ Can someone point out the errors or demonstrate a working example? Thank you.

$(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
  • `Its not working` is probably the worst info you can provide. Please give some more details. – C4d Apr 26 '16 at 13:50

1 Answers1

0

Try to use JSON.parse like this

$(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
Vaibhav Jain
  • 687
  • 4
  • 15
  • Here's the code I've been working on but I can't seem to generate an output. https://jsfiddle.net/Heropiggy95/6qkv7z3b/ I do not know what steps I should take to correct the problem. Could you advice me on how to troubleshoot this page? Thank you. – HeroPiggy95 Apr 27 '16 at 01:17
  • Are you trying to run this code on the localhost or any other domain., I tried to run it on local & it gives : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. You need to handle this on your server to allow your domain to send ajax requests. – Vaibhav Jain Apr 27 '16 at 08:59
  • I am trying to run it on localhost. I did my research and this suggests that 'Access-Control-Allow-Origin' can be bypassed by using ajax with jsonp. http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp Is it possible to use this method to get info from the server? – HeroPiggy95 Apr 27 '16 at 14:27