jQuery +获取.JSON +最后.FM问题

发布于 2024-08-17 13:38:39 字数 1962 浏览 9 评论 0原文

我正在尝试自定义 this 脚本,以便它不会显示用户最近的曲目用户最喜欢的艺术家。

这是我想出的办法,但它根本不起作用。我认为更改提要的属性很容易,但显然不是......

(function($){
$.fn.lastFM = function(options) {

var defaults = {
number: 10,
username: 'willblackmore',
apikey: '96e0589327a3f120074f74dbc8ec6443',
artSize: 'medium',
noart: 'images/noartwork.gif',
onComplete: function(){}
},
settings = $.extend({}, defaults, options);



var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user='+settings.username+'&api_key='+settings.apikey+'+'&limit='+settings.number+'&format=json&callback=?';
var $this = $(this);

var container = $this.html();

$this.children(':first').remove();

if(settings.artSize == 'small'){imgSize = 0}
if(settings.artSize == 'medium'){imgSize = 1}
if(settings.artSize == 'large'){imgSize = 2}

this.each(function() {

$.getJSON(lastUrl, function(data){ 
 $.each(data.topartists.artist, function(i, item){

 if(item.image[1]['#text'] == ''){
  art = settings.noart;
 }else{
  art = stripslashes(item.image[imgSize]['#text']);
 }

 url = stripslashes(item.url);
 song = item.name;
 artist = item.playcount['#text'];
 album = item.streamable['#text'];

 $this.append(container);

 var $current = $this.children(':eq('+i+')');

 $current.find('[class=lfm_song]').append(song);
 $current.find('[class=lfm_artist]').append(artist);
 $current.find('[class=lfm_album]').append(album);
 $current.find('[class=lfm_art]').append("<img src='"+art+"' alt='Artwork for "+album+"'/>");
 $current.find('a').attr('href', url).attr('title', 'Listen to '+song+' on Last.FM').attr('target', '_blank');

 //callback
 if(i==(settings.number-1)){
  settings.onComplete.call(this);
 }

  });
 });
});
};

 //Clean up the URL's
 function stripslashes( str ) {  
return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}
})(jQuery);`

有什么想法吗?谢谢。

I am trying to customize this script so that instead of a user's recent tracks it will display a user's favourite artists.

This is what I have come up with, however it doesn't work whatsoever. I thought it would be easy changing the attributes of the feed, but obviously not...

(function($){
$.fn.lastFM = function(options) {

var defaults = {
number: 10,
username: 'willblackmore',
apikey: '96e0589327a3f120074f74dbc8ec6443',
artSize: 'medium',
noart: 'images/noartwork.gif',
onComplete: function(){}
},
settings = $.extend({}, defaults, options);



var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user='+settings.username+'&api_key='+settings.apikey+'+'&limit='+settings.number+'&format=json&callback=?';
var $this = $(this);

var container = $this.html();

$this.children(':first').remove();

if(settings.artSize == 'small'){imgSize = 0}
if(settings.artSize == 'medium'){imgSize = 1}
if(settings.artSize == 'large'){imgSize = 2}

this.each(function() {

$.getJSON(lastUrl, function(data){ 
 $.each(data.topartists.artist, function(i, item){

 if(item.image[1]['#text'] == ''){
  art = settings.noart;
 }else{
  art = stripslashes(item.image[imgSize]['#text']);
 }

 url = stripslashes(item.url);
 song = item.name;
 artist = item.playcount['#text'];
 album = item.streamable['#text'];

 $this.append(container);

 var $current = $this.children(':eq('+i+')');

 $current.find('[class=lfm_song]').append(song);
 $current.find('[class=lfm_artist]').append(artist);
 $current.find('[class=lfm_album]').append(album);
 $current.find('[class=lfm_art]').append("<img src='"+art+"' alt='Artwork for "+album+"'/>");
 $current.find('a').attr('href', url).attr('title', 'Listen to '+song+' on Last.FM').attr('target', '_blank');

 //callback
 if(i==(settings.number-1)){
  settings.onComplete.call(this);
 }

  });
 });
});
};

 //Clean up the URL's
 function stripslashes( str ) {  
return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}
})(jQuery);`

Any ideas? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-08-24 13:38:39

您的网址连接错误。这是有效的:

var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=' + settings.username + '&api_key=' + settings.apikey + '&limit=' + settings.number + '&format=json&callback=?';

我不确定它是否能解决您的问题,因为您没有提供足够的详细信息(错误等)。

You have concencated your URL wrong. This is valid:

var lastUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=' + settings.username + '&api_key=' + settings.apikey + '&limit=' + settings.number + '&format=json&callback=?';

I'm not sure it fixes your problem since you didn't provide enough detail (errors etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文