javascript - jQuery ajax() using success, error and complete vs .done(), .fail() and always() -
the questions:
- should change our coding suggested below?
- is there difference between
.done()
&success:
,.fail()
&error:
,.always()
&complete:
?
the preamble:
i putting jquery.ajax call, have done in past too. this:
$.ajax( { url: someurl, type: 'post', data: somedata, datatype: 'json', success: function (data) { somesuccessfunction(data); }, error: function (jqxhr, textstatus, errorthrown) { someerrorfunction(); } });
while taking quick @ documentation, came across reference stating success, error , complete callbacks deprecated of jquery 1.8. prepare code eventual removal, use jqxhr.done(), jqxhr.fail(), , jqxhr.always() instead.
we should therefore start coding instead:
$.ajax( "example.php" ) .done(function (data) { somesuccessfunction(data); }) .fail(function (jqxhr, textstatus, errorthrown) { someerrorfunction(); }) .always(function() { alert("complete"); });
well there no advantage of doing in particular situation.
the point of .done()
.fail()
.always()
methods can
- attach multiple handlers
- do anywhere , not when calling
$.ajax
if @ $.ajax
call site attaching single handlers advantages don't come play.
so can return promise , others may attach own handlers.
example refreshing plugins after ajax request:
$.ajaxprefilter(function(opt, origopt, jqxhr) { jqxhr.always(function() { $("[data-plugin]").plugin(); }); });
Comments
Post a Comment