javascript - Jquery deferred not behaving as expected -
i make 2 ajax calls. second 1 should called when first finished:
var deferred = $.deferred(); firstajaxcall(); deferred.done(function () { secondajaxcall(); }); function firstajaxcall() { $.ajax({ url: '/someurl', type: 'post', success: function () { deferred.resolve(); } }); } function secondajaxcall() { $.ajax({ url: '/someotherurl', type: 'get', }); }
i tried (jquery deferreds)
$.when(firstajaxcall()).done(function() { secondajaxcall(); });
but no luck.
still, in first example, second call gets called first, doesn't
in first example flow this:
firstajaxcall(); secondajaxcall(); deferred.resolve();
why second call called first , before deferred.resolve()
?
you have return deferred $.ajax
$.when
make work
function firstajaxcall() { return $.ajax({ url : '/someurl', type : 'post' }); } function secondajaxcall(data_from_first) { return $.ajax({ url : '/someotherurl', type : 'get', }); } firstajaxcall().done(secondajaxcall);
Comments
Post a Comment