10

How can I view the last GET http request in JavaScript? Basically what I am after is what I can see my firebug console. When XMLHttpRequests are showing in console I see a line that looks something like:

GET   http://www.domain.com/php/file.php?q0&c=1   200   OK   163ms

How do I view that URL in JavaScript?

EDIT: Just to be clear I'm looking for the URL between GET... and ...200. I don't care about anything else. I don't want any of the other info.

3 Answers 3

15

You may want to modify the XMLHttpRequest.prototype.open to decorate it with your "tracking" code. Something like this:

var ajaxCalls = [];

XMLHttpRequest.prototype._originalOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
   ajaxCalls.push(url);
   this._originalOpen(method, url, async, user, password);
}

The ajaxCalls array will get populated with the URLs of your Ajax requests. The last request will be at ajaxCalls[ajaxCalls.length - 1].

Note that if you want to track only GET requests, you can simply log URLs only if method === 'GET' in the open() method.

1
  • Thanks, I ended up going with a shoddy workaround using $_SERVER['REQUEST_URI'] in PHP and then passing it to JS. Commented Sep 27, 2010 at 5:31
2

Daniel's solution works, but not for IE8 and lower, as IE has different number of arguments.

Try this instead:

window.XMLHttpRequest.prototype._original_open = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
    ajaxCalls.push(arguments[1]);
    this._original_open.apply(this,arguments);
};
-6

I ended up going with a workaround using $_SERVER['REQUEST_URI'] in PHP and then passing it to JavaScript.

Not the answer you're looking for? Browse other questions tagged or ask your own question.