如果你砍掉&callback=?
它的工作原理:
http://jsfiddle.net/Y44ZD/et
请注意在小提琴的示例中我如何收到错误消息:
error : function(e,d,f){
alert(f);
}
我得到“意外的令牌?”
http://api.jquery.com/jQuery.ajax/
说:
错误(jqXHR,textStatus,errorThrown)函数
请求失败时调用的函数。该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象、描述发生的错误类型的字符串和可选的异常对象(如果发生)。
您的代码将是:
$.ajax({
type:'GET',
url:'http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=megadeath&api_key=b5bd5e1d31bf4188a6bcd329c964f6f2&limit=5&format=json',
success : function(){
console.log("slurped up data");
},
complete: function(){
console.log("just completed the call");
},
error : function(){
console.log("something stinks in Denmark");
},
});
编辑(来自评论的问题):我必须阅读 last.fm 文档,但通常只需要在远程服务器要联系您的服务器并提供答案时在请求中指定回调。当响应您的请求返回数据时,此技术用于更安全的通信(例如与在线支付处理公司进行支付),这取决于您如何处理它,例如:
$.ajax({
type:'GET',
datatype:'json',
url:'http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=megadeath&api_key=b5bd5e1d31bf4188a6bcd329c964f6f2&limit=5&format=json',
success :
function(data){
$.each(data.results.artistmatches.artist, function(key, val) {
$('body').append('<a href="'+val.url+'">'+val.name + '</a><br />');
});
},
complete: function(){
alert("just completed the call");
},
error : function(e,d,f){
console.log(f);
},
});
这是那个例子:http: //jsfiddle.net/rS3p9/7/
(请注意,如果像我一样,您在解释 json 工具时遇到问题,例如:http: //jsonformatter.curiousconcept.com/,请很好地格式化它,这样您就可以看到发生了什么;-)
编辑:好的,我刚刚检查了文档:
http ://www.last.fm/api/show?service=272
他们没有提到回调:
limit (可选):每页要获取的结果数。默认为 50。
page (可选):要获取的页码。默认为第一页。
艺术家(必填):艺术家姓名
api_key(必需):Last.fm API 密钥。