jQuery.ajax handling continue responses: “success:” vs “.done”?

前端 未结 3 1594
情歌与酒
情歌与酒 2020-11-22 09:51

I have been working with jQuery and AJAX for a few weeks now and I saw two different ways to \'continue\' the script once the call has been made: success: and <

3条回答
  •  悲&欢浪女
    2020-11-22 09:59

    From JQuery Documentation

    The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information). These methods take one or more function arguments that are called when the $.ajax() request terminates. This allows you to assign multiple callbacks on a single request, and even to assign callbacks after the request may have completed. (If the request is already complete, the callback is fired immediately.) Available Promise methods of the jqXHR object include:

    jqXHR.done(function( data, textStatus, jqXHR ) {});
    

    An alternative construct to the success callback option, refer to deferred.done() for implementation details.

    jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
    

    An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

    jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); 
    

    (added in jQuery 1.6) An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

    In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

    jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {});
    

    Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details.

    Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

提交回复
热议问题