` element showing the target URL right before any AJAX request is sent:
```javascript
$(document).ajaxSend(function(event, xhr, options) {
$("div").append("
Requesting: " + options.url + "
"); }); ``` ### Example 2: Adding a Global Authorization Header You can use the `xhr` parameter to dynamically inject headers (such as a Bearer Token) into every outgoing AJAX request: ```javascript $(document).ajaxSend(function(event, xhr, options) { const token = localStorage.getItem("authToken"); if (token) { xhr.setRequestHeader("Authorization", "Bearer " + token); } }); ``` ### Example 3: Showing a Loading Spinner A common real-world use case is displaying a loading indicator when an AJAX request starts: ```javascript $(document).ajaxSend(function() { $("#loading-spinner").show(); }); ``` --- ## Key Considerations 1. **Global Scope:** The `ajaxSend()` method is a global handler. It will trigger for *every* AJAX request made on the page using jQuery (e.g., `$.ajax`, `$.get`, `$.post`). 2. **Disabling Globally:** If you have a specific AJAX request that should *not* trigger global handlers like `ajaxSend()`, you can set the `global` option to `false` in that specific request: ```javascript $.ajax({ url: "example.php", global: false // This prevents ajaxSend from firing for this request }); ``` 3. **Execution Order:** `ajaxSend()` runs after the request is initialized but before it is actually transmitted over the network.
YouTip