Although success has been the traditional form of successful callback in jQuery, however, since implementation $.Deferreds and callbacks more sophisticated, done is the preferred way to implement successful callbacks.In the jQuery documentation we read the following:Disqualification notice: Callbacks jqXHR.success
(), jqXHR.error () and jqXHR.complete () are eliminated from
jQuery 3.0. You can use jqXHR.done (), jqXHR.fail () and
jqXHR.always () instead.This means that from jQuery 3 it is recommended to use done.Why use done ?Because the jqXHR objects returned by $ .ajax () from jQuery 1.5 implement the Promise interface, giving them all the properties, methods and behavior of a Promise (see the object http://api.jquery.com/category/deferred-object/ for more information). These methods take one or more function arguments that are called when the application ends $ .ajax (). This allows you to assign several call returns in one request and even assign call returns after the application has been completed. (If the request is complete, the callback will be fired immediately.) The available Promise methods of the jqXHR object include:jqXHR.done(function( data, textStatus, jqXHR ) {});An alternative construction to the successful callback option, see http://api.jquery.com/deferred.done/ for implementation details.jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});An alternative construction to error callback option, method .fail () replaces the outdated .error () method. Consult http://api.jquery.com/deferred.fail/ for details on implementation.jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); (added in jQuery 1.6)
An alternative construction to the full call return option, method .always () replaces the obsolete method .complete ().In response to a satisfactory request, the arguments of the function are the same as those of .done (): data, textStatus and jqXHR object. For mistaken applications, arguments are the same as those of .fail (): the object jqXHR, textStatus and errorThrown. Consult http://api.jquery.com/deferred.always/ for details on implementation.jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {});Incorporates the functionality of the methods .done () and .fail (), allowing (from jQuery 1.8) that the underlying Promise is manipulated. Consult http://api.jquery.com/deferred.then/ for details on implementation.The good thing done is that the return value of $.ajax is now a deferred promise that may be linked to any other place in its application. So let's say you want to make this ajax call from a few different places. Instead of passing your success function as an option to the function that makes this ajax call, you can only have the return function $.ajax and link your call returns to done, fail, thenOr whatever. Note that it is always a callback that will run if the request is successful or fails. done will only be activated when the ajax request is successful.Complete example using the above:// Se asignan manejadores inmediatamente después de hacer la petición
// y se recuerda el objeto jqXHR para esta petición
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "èxito" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "completado" );
});
// Hacer otra cosa aquí ...
// Asignar otra función de completado para la petición de más arriba
jqxhr.always(function() {
alert( "completado segundo" );
});
Another example:function xhr_get(url) {
return $.ajax({
url: url,
type: 'get',
dataType: 'json',
beforeSend: mostrarImagenCargando
})
.always(function() {
// Por ejemplo removemos la imagen "cargando..."
})
.fail(function() {
// Manejar errores
});
}
xhr_get('/index').done(function(data) {
// Hacer algo con data
});
xhr_get('/id').done(function(data) {
// hacer algo con el id de data
});
An important benefit of this in terms of maintenance is that it has wrapped its ajax mechanism into a specific function of the application. If you decide you need your call $.ajax to operate differently in the future, or use a different method of ajax, or get away from jQuery, you just have to change the definition of xhr_get (to be sure to return a promise or at least one method donein the case of the example above. All other references throughout the application may remain the same.There are many more things (much cooler) you can do with $.Deferred, one of which is to use pipe (or better) then) to activate an error in an error reported by the server, even when the request $.ajax is successful.
For example:function xhr_get(url) {
return $.ajax({
url: url,
type: 'get',
dataType: 'json'
})
.pipe(function(data) {
return data.responseCode != 200 ?
$.Deferred().reject( data ) :
data;
})
.fail(function(data) {
if ( data.responseCode )
console.log( data.responseCode );
});
}
xhr_get('/index').done(function(data) {
// No funcionará si el json retornado desde ajax no tiene el código de respuesta 200
});
Sources: http://api.jquery.com/jquery.ajax/ https://stackoverflow.com/a/8840315/5587982