Is there a way to intercept all AJAX requests in JavaScript?

Could there be a universal event handler for ajax requests that is automatically triggered upon the completion of any ajax request?

This is something I am exploring while developing a greasemonkey script for an ajax-heavy website. In past scripts, I have resorted to running the main function at intervals or modifying portions of the site's javascript, both of which have proved to be messy solutions.

Answer №1

It is not possible with standard XMLHttpRequest. Certain libraries, such as jQuery, offer solutions like wrappers (e.g. ajaxComplete) for this purpose. These wrappers will only be triggered for ajax requests that also utilize the wrapper (e.g. jQuery.ajax).

Answer №2

With the use of jQuery, I am able to achieve the following:

$(document).ready(function() {
    $("#contentLoading").hide();

    $("#contentLoading").ajaxSend(function(r,s){
        $(this).show();
        $("#ready").hide();
    });

    $("#contentLoading").ajaxStop(function(r,s){
        $(this).hide();
        $("#ready").show();
    });

Rather than toggling the visibility of elements, a more generic function could be implemented for efficiency.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking for guidance on how to specify the angular direction in the ng-repeat

I am facing a challenge with the structure of my ng-repeat directive and I am having trouble navigating through it. Here is a representation of the data in array format JSON style {"data":[ ["sameer","1001", [ {"button_icon": ...

Retrieve data from dropdown menu to showcase table using Node.js

I'm currently diving into learning nodejs and mongodb. My backend setup includes expressjs and mongojs, while ejs is handling the frontend of my application. The main goal is to allow users to select a class from a dropdown menu and view a correspondi ...

how to set a boolean value to true in a vue @click event?

@click.native="scrollTo(index,true)" My expectation: Always pass Boolean:true into the scrollTo function. Vue's reaction: Vue interprets true as a variable name, resulting in Number:index and undefined instead. Solution: ...

Bootstrap 4 Collapse - Ensuring that collapsed elements remain open when expanding other accordions

Can someone help me figure out how to keep an element open when another one is opened? Here is an example: https://getbootstrap.com/docs/4.0/components/collapse/ <div id="exampleAccordion" data-children=".item"> <div class="item"> & ...

Adjust the DOM based on the output of the function

I'm currently working on creating a list where only one element can be active at a time. The state is updating correctly, but I'm facing an issue with the isActive function. It only activates initially and doesn't trigger when the state chan ...

Ways to eliminate the white overlay that occurs on the page after clicking the scroll top button

Hi there! I've been busy with updating my site at , and noticed a small glitch. Every time I hit the Back to Top button at the bottom of the page, the screen goes white. Any ideas on how to fix this issue? Thanks in advance! ...

Combining two arrays in a view and then storing them in a database using Codeigniter

Hello everyone, I am a new member on this platform... Currently, I am working with the codeigniter framework and have two separate arrays in my view. The first array includes data from a form input named customerData, customerData = { customerid ...

What separates the onclick functionality of buttons from that of divs?

Initially, this issue may be specific to Firefox, though it is uncertain. Let's delve into some code: <!doctype html> <html> <head> <meta charset="UTF-8> <title>Text Editor</title> <!-- Custom CSS -- ...

Ways to adjust the vAxis and hAxis labels to display as whole numbers

Below is the code provided: var rawdata='".$performances."'; var mydata=jQuery.parseJSON(rawdata); if(mydata){ var realData=[]; realData=[ ['Activities', &a ...

Send a form without the need to reload the page

I have a form that inserts values into the database successfully. However, each time the values are submitted, the page refreshes. I attempted to find a solution on this website, but it didn't work for me. Can someone please advise me on how to submit ...

What purpose does the array.pop()!(object) syntax serve within Codemirror?

Within the Codemirror 6 documentation and at line 41 of the code, there is a snippet that reads: while (pending.length) pending.pop()!(data.updates) I'm curious about the meaning of this syntax. It appears to be TypeScript specific. How would this lo ...

What is the best way to insert a row into a JavaScript table while incorporating a cell that utilizes the datepicker function in jQuery?

Currently, I am working on a javascript function: function addItem(tableID){ var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var el ...

How can the outer function be connected to the resolve method of $routeProvider?

Here is a functional code snippet: $routeProvider.when('/clients', { templateUrl:'/views/clients.html', controller:'clientsController', resolve: { rights: function ( ...

There was an issue converting the value {null} to the data type 'System.Int32', resulting in a 400 error

Attempting to make a POST request with some missing data is causing errors in my angular form. Here is the payload I am using: DeviceDetail{ deviceId:'332', sideId: null, deviceName:'test' } Unfortunately, I encountered a 400 bad re ...

Extracting JSON data from a string using jQuery

http://localhost/project1/index.php //AJAX region $(function(){ $.ajax({ type : 'GET', url : 'https://jsonplaceholder.typicode.com/posts/1', success : function(data){ console.log('success \n', data); }, error ...

Having trouble decoding URL in nodeJS

I have encountered an issue while using the express framework in NodeJS. I am attempting to send an activation link via email, but when activating the link, the URL becomes encoded and the express routes are unable to read it, leading to a 404 error page. ...

Tips for efficiently utilizing AJAX requests in conjunction with PHP without duplicating code

I have a small script that utilizes AJAX and PHP to showcase an image. As you can see, by calling the mom() function, it searches the PHP file index.php?i=mom and displays the desired image. However, I am looking for a way to streamline the JavaScript cod ...

Deleting a file in Express.js after it has been downloaded from the local file system

I'm working on an Express handler that is designed to download a file after detecting a valid file path in the directory entries. The handler includes a command-line option to delete the file after it has been successfully downloaded: res.downloa ...

React's POST request is not received by Node

Here is the React code I am using to post: const [nickname, setNickname] = useState('') const [title, setTitle] = useState('') const [content, setContent] = useState('') const [agree, setAgree] = useState(false) const [thepost ...

Circular reference in Angular/TypeScript

I encountered a circular dependency issue in my Angular project and tried various solutions, including exporting all dependent classes from a "single file" as suggested here. Unfortunately, this approach did not work for me. I then explored other solutions ...