简体   繁体   中英

which one should be used in case of jquery ajax call, success/ error callback or done()/fail() chain function

sometimes even if the ajax call has returned error, it calls done() chain function rather than fail(). so confused whether to use success/error callback or done()/fail() chain function.

for eg which one is advisable 1 or 2?

  1.  $.ajax({ url: someurl, success: function(){ //some code if ajax request is successful }, error: function(){ //some code if ajax request fails } }) 

  2.  $.ajax({ url: someurl }).done(function(){ //some code changes }).fail(function(){ //some code changes }); 

These are interchangeable ways of doing an AJAX call in jQuery. The second method you describe uses promises, whereas the first method handles successes/errors as options on the AJAX call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM