How to cancel an ongoing AJAX request in jQuery?

To cancel an ongoing AJAX request in jQuery, you can use the abort() method provided by the XMLHttpRequest object. Here is an example:

var xhr = $.ajax({ url: "your-url", method: "GET", beforeSend: function() { // Do something before the request is sent }, success: function(response) { // Handle successful response }, error: function(xhr, status, error) { // Handle error } }); // To cancel the request xhr.abort();

In this example, the $.ajax() method returns an object that provides access to the underlying XMLHttpRequest object. By calling the abort() method on this object, you can cancel the ongoing request.

Note that the error() callback will be called with the textStatus parameter set to "abort" when the request is cancelled.