Ajax Ajaxsuccess
## jQuery ajaxSuccess() Method
The `ajaxSuccess()` method is a global AJAX event handler in jQuery. It specifies a callback function to be executed whenever an AJAX request completes successfully.
This is highly useful for implementing centralized logging, updating global UI indicators (such as hiding a loading spinner), or tracking analytics across all AJAX requests in your application.
---
## Definition and Usage
The `ajaxSuccess()` method registers a listener that is triggered every time an AJAX request finishes successfully.
> **Important Note:** As of jQuery version 1.8, this method **must only be attached to the `document` object**. Attaching it to other DOM elements will not work in modern versions of jQuery.
---
## Syntax
```javascript
$(document).ajaxSuccess(function(event, xhr, options))
```
### Parameter Values
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `function(event, xhr, options)` | `Function` | **Required.** The callback function to run when an AJAX request succeeds. |
### Callback Function Parameters
When the callback function is triggered, it receives three arguments:
* **`event`**: The jQuery Event object.
* **`xhr`**: The native `XMLHttpRequest` object containing the response data and status.
* **`options`**: An object containing the configuration options used for the AJAX request (e.g., `url`, `type`, `data`).
---
## Code Examples
### Example 1: Basic Usage
Trigger an alert message whenever any AJAX request on the page completes successfully:
```javascript
$(document).ajaxSuccess(function() {
alert("An AJAX request has completed successfully!");
});
```
### Example 2: Using the `options` Parameter to Target Specific Requests
In real-world applications, you often want to run code only when a *specific* AJAX request succeeds. You can filter requests by inspecting the `options` parameter:
```javascript
$(document).ajaxSuccess(function(event, xhr, options) {
// Check if the successful request was sent to "demo_test.txt"
if (options.url === "demo_test.txt") {
console.log("The specific file 'demo_test.txt' was loaded successfully.");
}
});
```
### Example 3: Accessing Response Data via the `xhr` Parameter
You can read the response text or headers returned by the server using the `xhr` object:
```javascript
$(document).ajaxSuccess(function(event, xhr, options) {
// Parse the JSON response returned from the server
try {
var responseData = JSON.parse(xhr.responseText);
console.log("Data received from " + options.url + ":", responseData);
} catch (e) {
console.log("Response was not JSON format.");
}
});
```
---
## Considerations & Best Practices
1. **Global Scope:** Because `ajaxSuccess()` is a global handler, it will trigger for *every* successful AJAX request initiated by jQuery on the page (using `$.ajax`, `$.get`, `$.post`, etc.).
2. **Disabling Globally:** If you have a specific AJAX request that you do *not* want to trigger global handlers like `ajaxSuccess()`, you can set the `global` option to `false` in that specific request:
```javascript
$.ajax({
url: "private-api.php",
global: false, // Prevents ajaxSuccess from triggering
success: function() {
// Local success callback only
}
});
```
3. **Deprecation Warning for Non-Document Elements:** Always bind this method to `$(document)`. Writing code like `$("#my-div").ajaxSuccess(...)` is deprecated and will fail in modern jQuery environments.
YouTip