javascript - jQuery ajax() using success, error and complete vs .done(), .fail() and always() -


the questions:

  1. should change our coding suggested below?
  2. 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

  1. attach multiple handlers
  2. 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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -