JavaScript Answers

How to Run Code After jQuery Ajax Calls Regardless of the Result?

Spread the love

To run code after jQuery Ajax calls regardless of the result, we can call the always method.

For instance, we can write:

  url: '',  
  dataType: 'json',  
  type: 'GET'  
}).then((processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) => {  
}).always(() => {  
  console.log("always run");  

We call ajax with an object that has the url of the request.

dataType is set to 'json' to make a JSON request.

type is set to 'GET' to make a GET request.

Then we call then with a callback to get the response of the request when it’s successful.

And then we call always with a callback that runs regardless of the request result.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.