开发者

last.fm json call error

the following code:

    $.ajax({
        type:'GET',
        url:'http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=megadeath&api_key=b5bd5e1d31bf4188a6bcd329c964f6f2&limit=5&format=json&callback=?',
        success : function(){
            console.log("slurped up data");
        },
        complete: function(){
            console.log("just completed the c开发者_运维知识库all");
        },
        error : function(){
            console.log("something stinks in Denmark");
        },
    });

throws an error. When I look at the REST call in the browser I see that it actually produces output, so I think the error pops up in the way the data is handled by jQuery. Have folks encountered this problem before? any suggestions on how to handle this issue?


if you chop &callback=? off the end it works:

http://jsfiddle.net/Y44ZD/et

note how in the example on fiddle I get the error message with:

    error : function(e,d,f){
        alert(f);
    }

I was getting "unexpected token ?"

http://api.jquery.com/jQuery.ajax/

says:

error(jqXHR, textStatus, errorThrown)Function

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred.

Your code would be:

$.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");
    },
});

EDIT (Question from comment): I would have to read the last.fm documentation but normally callbacks only need to be specified in the request when the remote server is going to contact your server with the answer. this technique is used for more secure communication (e.g. making a payment with an online payment processing company) when the data is return in response to your request its up to you how you process it e.g.:

$.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);
    },
});

here is that example: http://jsfiddle.net/rS3p9/7/

(note if like me you are having trouble interpreting json tools like this help: http://jsonformatter.curiousconcept.com/ thay format it nicely so you can see whats going on ;-)

EDIT: okay I just checked the docs: http://www.last.fm/api/show?service=272

they dont mention a callback:

limit (Optional) : The number of results to fetch per page. Defaults to 50.

page (Optional) : The page number to fetch. Defaults to first page.

artist (Required) : The artist name

api_key (Required) : A Last.fm API key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜