如何使用Api添加last.fm现在播放到我的网站

时间:2015-04-12 14:45:21

标签: javascript jquery api last.fm

嗨,大家好我一直想弄清楚但是我很尴尬,我发现这个代码在谷歌上我添加了它改变了我需要但仍然无法正常工作我真的需要这个为我的网站:{{ 3}}

我在这里找到了代码:http://www.balkan-party.cf/

我在js中需要的last.fm用户名:alicajdin AND Api键:24f6b03517ad9984de417be5d10e150b

这就是我所做的:



$(document).ready(function() {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=alicajdin&api_key=24f6b03517ad9984de417be5d10e150b&limit=2&format=json&callback=?", function(data) {
        var html = ''; // we declare the variable that we'll be using to store our information
        var counter = 1; // we declare a counter variable to use with the if statement in order to limit the result to 1
        $.each(data.recenttracks.track, function(i, item) {
            if(counter == 1) {
                html += 'Currently listening to: <span><a href="' + item.url + '" target="_blank">' + item.name + '</a> - ' + item.artist['#text'] + '</span>';
            } // close the if statement
            counter++ // add 1 to the counter variable each time the each loop runs
        }); // close each loop
        $('.listening-to h5').append(html); // print the information to the document - here I look for the h5 tag inside the div with a class of 'listening-to' and use the jQuery append method to insert the information we've stored in the html variable inside the h5 tag.
    }); // close JSON call
});
&#13;
&#13;
&#13;

我创建了该文件,我试图添加头部分,页脚部分,但它不会显示最近的曲目。 是的,我在谷歌crome上安装了scroblr

1 个答案:

答案 0 :(得分:1)

下面</script>添加以下代码:

<div class="listening-to"></div>

然后删除

上的“h5”
"$('.listening-to h5').append(html);"

所以你的代码是这样的:

<script type="text/javascript">
    $(document).ready(function() {
        $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=YOUR_USERNAME&api_key=YOUR_API_KEY&limit=2&format=json&callback=?", function(data) {
            var html = '';
            var counter = 1;
            $.each(data.recenttracks.track, function(i, item) {
                if(counter == 1) {
                    html += 'Currently listening to: <span><a href="' + item.url + '" target="_blank">' + item.name + '</a> - ' + item.artist['#text'] + '</span>';
                }
                counter++
            });
            $('.listening-to').append(html);
        });
    });
</script>
<div class="listening-to"></div>

希望你能提供帮助。对不起,我的英语非常糟糕(谷歌翻译)