dandlezzz
dandlezzz

Reputation: 613

jquery ajax request spotify api

Why doesn't this work? I can get the request function to work when I call it without the click handler. But for some reason when I call it with the click handler it won't go.

var aRequest = function(){
    $.ajax({
        url: 'http://ws.spotify.com/search/1/album?q=hello'
    });
}

$(document).ready(function(){
    $('h1').click(aRequest);
});

Upvotes: 0

Views: 3070

Answers (1)

Hattan Shobokshi
Hattan Shobokshi

Reputation: 687

Edit: I forgot to mention, earlier you were grabbing XML data, if you want to use jquery it's often easier to just get the response as json. You can do this in the spotify api by appending .json to the requested resource.

The code above seems to be working for me. Make sure that you have jquery referenced before that block.

Here's a fiddle with a working sample: http://jsfiddle.net/JfaEc/

var aRequest = function(){
    $.ajax({
        url: 'http://ws.spotify.com/search/1/album.json?q=hello'
    }).success(function(response){
    var albums = response.albums;
    var result="";
    for(var i=0,len=albums.length;i<len;i++){
        var album = albums[i];
        result= result + "<li>" + album.name + "</li>";
    }
    $("#results").html(result);
});

}

$(document).ready(function(){
    $('h1').click(aRequest)
})

Upvotes: 1

Related Questions