Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JQuery listen to AJAX calls from other javascript?

I need to build a feature into a shopping cart that uses AJAX to retrieve an updated copy of the template from the server when something changes (e.g. a product is removed). I cannot modify the server side code, or the JavaScript that makes the shopping cart work in the first place. (Not ideal I know, but that's how it is)

What I want to do is run my own JavaScript every time the cart updates. I want to know if it is possible to listen for AJAX calls, and run my code every time one is made.

like image 668
BudgieInWA Avatar asked Dec 10 '10 07:12

BudgieInWA


People also ask

Is jQuery compatible with AJAX?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Can I make an AJAX call from JavaScript?

Another way to make AJAX calls in JavaScript is with the fetch() method. fetch() is an API utility method built into the web browser environment. It's a newer API than XMLHttpRequest , with modern features making it easier to use. I recommend you use fetch() for AJAX.

Is AJAX dependent on JavaScript?

Ajax is dependent on Javascript. If there is some Javascript problem with the browser or in the OS, Ajax will not support. Ajax can be problematic in Search engines as it uses Javascript for most of its parts.

Does AJAX follow redirect?

ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it? There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.


1 Answers

To follow all AJAX calls on an HTML doc, you can overwrite the XMLHttpRequest prototype. This way, you can watch for actions on methods of XMLHttpRequest objects.

Here's a small sample code :

var open = window.XMLHttpRequest.prototype.open,     send = window.XMLHttpRequest.prototype.send,     onReadyStateChange;  function openReplacement(method, url, async, user, password) {     var syncMode = async !== false ? 'async' : 'sync';     console.warn(         'Preparing ' +         syncMode +         ' HTTP request : ' +         method +         ' ' +         url     );     return open.apply(this, arguments); }  function sendReplacement(data) {     console.warn('Sending HTTP request data : ', data);      if(this.onreadystatechange) {         this._onreadystatechange = this.onreadystatechange;     }     this.onreadystatechange = onReadyStateChangeReplacement;      return send.apply(this, arguments); }  function onReadyStateChangeReplacement() {     console.warn('HTTP request ready state changed : ' + this.readyState);     if(this._onreadystatechange) {         return this._onreadystatechange.apply(this, arguments);     } }  window.XMLHttpRequest.prototype.open = openReplacement; window.XMLHttpRequest.prototype.send = sendReplacement; 

With this sample, for every AJAX call you'll have a warning in the JavaScript console.

It's not jQuery script, but you can use jQuery inside as you want.

This solution probably won't work on IE 6 or older, but it works in FF, IE7+, Chrome, Opera, Safari...

like image 137
Nicolas Avatar answered Oct 04 '22 20:10

Nicolas