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

后端 未结 1 1580
余生分开走
余生分开走 2021-01-30 19:53

The questions:

  1. Should we change our coding as suggested below?
  2. Is there a difference between .done() & success:
相关标签:
1条回答
  • 2021-01-30 20:00

    Well there is no advantage of doing that in that particular situation.

    The point of the .done() .fail() .always() methods is that you can

    1. Attach multiple handlers
    2. Do so anywhere and not just when calling $.ajax

    If you are at the $.ajax call site only attaching single handlers then those advantages don't really come into play.

    So you can return the promise and others may attach their own handlers.

    Example is refreshing plugins after ajax request:

    $.ajaxPrefilter(function(opt, origOpt, jqxhr) {
        jqxhr.always(function() {
            $("[data-plugin]").plugin();
        });
    });
    
    0 讨论(0)
提交回复
热议问题