Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created October 14, 2012 19:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save alecperkins/3889507 to your computer and use it in GitHub Desktop.
Save alecperkins/3889507 to your computer and use it in GitHub Desktop.
Detect any XMLHttpRequests
// From: http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event
var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
// this.method :the ajax method used
// this.url :the url of the requested script (including query string, if any) (urlencoded)
// this.data :the data sent, if any ex: foo=bar&a=b (urlencoded)
console.log(this.method);
console.log(this.url);
console.log(this.data);
}
XMLHttpRequest.prototype.open = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempOpen.apply(this, arguments);
s_ajaxListener.method = a;
s_ajaxListener.url = b;
if (a.toLowerCase() == 'get') {
s_ajaxListener.data = b.split('?');
s_ajaxListener.data = s_ajaxListener.data[1];
}
}
XMLHttpRequest.prototype.send = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempSend.apply(this, arguments);
if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
s_ajaxListener.callback();
}
@Petar-Luketina
Copy link

Thank you, that was very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment