Tags
jQuery , AJAX
Asked 3 years ago
24 Jun 2021
Views 751
Mafalda

Mafalda posted

.done after AJAX callback in jQuery

.done after AJAX callback in jQuery
kiran

kiran
answered Apr 28 '23 00:00

jQuery provides the .done() method that is used to add a callback function to a deferred object. This function executes when the deferred object is resolved, which is usually after a successful AJAX request. You can use this method to execute a piece of code after an AJAX callback in jQuery, as shown in the following example:



$.ajax({
  url: 'example.com/ajax',
  dataType: 'json'
}).done(function(response) {
  // Handle the successful response
  console.log(response);
}).fail(function(xhr, status, error) {
  // Handle the error response
  console.log(error);
}).always(function() {
  // Handle the completion
  console.log('The AJAX request is completed');
});

In this example, the .done() method is used to handle the successful response of the AJAX request. The function takes the response argument, which is the data returned by the server. Similarly, the . fail () method is used to handle any error response from the server, while the . always() method is used to handle the completion of the AJAX request, regardless of its success or failure.

It is worth noting that .done() is just one of several methods that can be used to handle AJAX callbacks in jQuery. Other methods include .success(), .error(), and .complete(). However, starting from jQuery 3.0, the .success() and . error() methods have been deprecated in favor of the more versatile .done() and . fail() methods.
Post Answer