Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax handling continue responses: "success:" vs ".done"?

Tags:

jquery

ajax

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 .done.

From the synopsis from the jQuery documentation we get:

.done(): Description: Add handlers to be called when the Deferred object is resolved.

success: (.ajax() option): A function to be called if the request succeeds.

So, both do something after the AJAX call has been completed/resolved. Can I use one or the other randomly? What is the difference and when one is used instead of the other?

like image 234
Claudio Avatar asked Jan 12 '12 18:01

Claudio


People also ask

What is difference between success complete in jQuery AJAX?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.

Is AJAX successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.

What does success mean in AJAX?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

How do I return data after AJAX call success?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });


2 Answers

success has been the traditional name of the success callback in jQuery, defined as an option in the ajax call. However, since the implementation of $.Deferreds and more sophisticated callbacks, done is the preferred way to implement success callbacks, as it can be called on any deferred.

For example, success:

$.ajax({   url: '/',   success: function(data) {} }); 

For example, done:

$.ajax({url: '/'}).done(function(data) {}); 

The nice thing about done is that the return value of $.ajax is now a deferred promise that can be bound to anywhere else in your application. So let's say you want to make this ajax call from a few different places. Rather than passing in your success function as an option to the function that makes this ajax call, you can just have the function return $.ajax itself and bind your callbacks with done, fail, then, or whatever. Note that always is a callback that will run whether the request succeeds or fails. done will only be triggered on success.

For example:

function xhr_get(url) {    return $.ajax({     url: url,     type: 'get',     dataType: 'json',     beforeSend: showLoadingImgFn   })   .always(function() {     // remove loading image maybe   })   .fail(function() {     // handle request failures   });  }  xhr_get('/index').done(function(data) {   // do stuff with index data });  xhr_get('/id').done(function(data) {   // do stuff with id data }); 

An important benefit of this in terms of maintainability is that you've wrapped your ajax mechanism in an application-specific function. If you decide you need your $.ajax call to operate differently in the future, or you use a different ajax method, or you move away from jQuery, you only have to change the xhr_get definition (being sure to return a promise or at least a done method, in the case of the example above). All the other references throughout the app can remain the same.

There are many more (much cooler) things you can do with $.Deferred, one of which is to use pipe to trigger a failure on an error reported by the server, even when the $.ajax request itself succeeds. 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) {   // will not run if json returned from ajax has responseCode other than 200 }); 

Read more about $.Deferred here: http://api.jquery.com/category/deferred-object/

NOTE: As of jQuery 1.8, pipe has been deprecated in favor of using then in exactly the same way.

like image 103
12 revs Avatar answered Sep 24 '22 06:09

12 revs


If you need async: false in your ajax, you should use success instead of .done. Else you better to use .done. This is from jQuery official site:

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().

like image 37
AmirHossein Manian Avatar answered Sep 24 '22 06:09

AmirHossein Manian