嗨,大家好,我很久以来一直想弄清楚,但是我很讨厌,我在google上找到了这段代码,并添加了它并更改了我需要的内容,但仍然无法正常工作,我的网站确实需要这个:http://www.balkan-party.cf/

我在这里找到代码:http://www.samkitson.co.uk/using-json-to-access-last-fm-recently-played-tracks/

我在js中需要的last.fm用户名:alicajdin和
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
});





我创建了该文件,并尝试在页眉部分,页脚部分添加内容,但它不会显示最近的曲目。
是的,我在Google Crome上安装了scroblr

最佳答案

</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>


希望能对您有所帮助。抱歉,我的英语不好(Google翻译)

关于javascript - 现在如何使用Api将last.fm添加到我的网站中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29590702/

10-12 12:21
查看更多