javascript - 如何使用 Api 将正在播放的 last.fm 添加到我的网站中

标签 javascript jquery api last.fm

大家好,我一直想弄清楚,但我对此很烂,我在谷歌上找到了这段代码,我添加了它并更改了我需要的内容,但仍然不起作用,我真的需要这个用于我的网站:http://www.balkan-party.cf/

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

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

我创建了该文件,并尝试添加头部部分、页 footer 分,但它不会显示最近的轨道。 是的,我在 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>

希望能帮到你。抱歉,我的英语很差(谷歌翻译)

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

相关文章:

android - Android 上的微软必应翻译 API

rest - 为什么发布到 PayPal 沙盒 API 对我不起作用?

javascript - 在 map 框中过滤标记后,弹出窗口不再起作用

javascript - Javascript 数组中的循环

javascript - 使用 javascript 和 jquery ui 单击事件的 css

javascript - 如何判断 HTML 文本区域何时被 Javascript 更改

javascript - 在 Bootstrap 中禁用复选框标签并将其灰显

python - 如何保持 python 3 脚本(Bot)运行

javascript - 理解memo函数中的resolver(大前端q)

JavaScript。检查是否没有焦点选项卡