I wanted to fire a function when multiple ajax calls are completed. After some research I found the jquery $.when function. I tested this function but it doesn't work. Does anybody know how to solve this?
var root = 'http://jsonplaceholder.typicode.com';
$(function(){
restCall.ajaxcalls();
})
var restCall =
{
ajaxcalls: function(){
$.when(this.getAlbums(), this.getPhotos()).done(function(fetchedAlbums,fetchedPhotos){
console.log(fetchedAlbums);
console.log(fetchedPhotos);
});
},
getAlbums:function() {
$.ajax({
type: 'GET',
url: root + '/albums'
}).done(function(response){
return response;
}).fail(this.ajaxFail);
},
getPhotos: function(){
$.ajax({
type: 'GET',
url: root + '/photos'
}).done(function(response){
return response;
}).fail(this.ajaxFail);
},
ajaxFail: function(xhr,message,error){
console.log("het liep mis met de ajax call",xhr,message,error);
}
};
The console logs returns undefined, but I want the objects that were fetched by the ajax call.
Does anybody see where it went wrong?