Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view the last GET HTTP request in JavaScript

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.

like image 645
themerlinproject Avatar asked Sep 26 '10 18:09

themerlinproject


People also ask

How get data from HTTP GET?

The HTTP GET request method is used to request a resource from the server. The GET request should only receive data (the server must not change its state). If you want to change data on the server, use POST, PUT, PATCH or DELETE methods.

DOES GET request JavaScript?

To send a GET request using vanilla JavaScript, you must use the XMLHttpRequest object to interact with the server. You need to create an XMLHttpRequest object, open the URL and send the request. After the request is made, the response body can be retrieved and processed using the xhr.

How do you intercept HTTP request in JavaScript?

To intercept HTTP requests, use the webRequest API. This API enables you to add listeners for various stages of making an HTTP request. In the listeners, you can: Get access to request headers and bodies and response headers.

How do I track browser requests?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.


2 Answers

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);
};
like image 45
alex23 Avatar answered Nov 15 '22 07:11

alex23


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.

like image 84
Daniel Vassallo Avatar answered Nov 15 '22 09:11

Daniel Vassallo