Creating/Accessing a JSON object using jQuery $.ajax with Last.FM API

前端 未结 2 398
清酒与你
清酒与你 2021-01-03 02:35

I\'ve recently changed my site design and now need to use dynamic AJAX requests for my data. Basically, I\'m trying to retrieve user data using the Last.FM API in JSON forma

相关标签:
2条回答
  • 2021-01-03 03:05

    The html variable must be declared outside the scope of the each:

      //var topArt;
     $(document).ready(function() {
        $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?", function(data) {
            var html = '';
            $.each(data.topartists.artist, function(i, item) {
                html += "<p>" + item.name + " - " + item.playcount + "</p>";
            });
            $('#test').append(html);
             // topArt = data.topartists;
        });
    });
    

    As for your second question, you'll need a global variable. You can put it before $(document).ready() (as shown in the comment) and it will be accessible everywhere.

    0 讨论(0)
  • 2021-01-03 03:19

    I tried the following URL what you are using "http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?"

    It gives not-well-formed json but if I use following URL "http://ws.audioscrobbler.com/2.0/?method=user.getTopArtists&user=test&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json"

    I got the correct JSON.

    Also you'll have to declare html as the response given above.

    0 讨论(0)
提交回复
热议问题