tags:

views:

36

answers:

2

Basically what the title says, I want to get the URL and HTTP Verb from a xhr. Is this possible?

+1  A: 

Not natively, I'm afraid. In prototypable implementations you could write your own prototype:

XMLHttpRequest.prototype.__oldOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.verb = "";
XMLHttpRequest.prototype.url  = "";
XMLHttpRequest.prototype.open = function (verb, url, async)
{
    this.verb = verb;
    this.url  = url;
    this.__oldOpen.call(this, verb, url, async); 
}

Don't expect it to work in IE7 and older though.


I suppose you could do it by completely recreating the XMLHttpRequest object, but it would take a lot of work to get it right:

var oldXHR = XMLHttpRequest;
function XMLHttpRequest()
{
    var realXHR = new oldXHR();
    this.onreadystatechange = function () {}
    this.open = function (verb, url, async)
    {
        this.verb = verb;
        this.url  = url;
        realXHR.open(verb, url, async);
    {
    this.send = function () { realXHR.send(); }

    // all other properties and methods... 
}

Of course, you have to go to the effort of correctly binding onreadystatechange and setting the status, etc.

Andy E
Nice workaround (+1), I need it to be crossbrowser though...
Pablo Fernandez
@Pablo: You could possibly recreate the entire XMLHttpRequest object to override it instead. See my update.
Andy E
+1  A: 
Currently, there is no standard way to get HTTP verb or url from XHR object. But, W3C is considering getRequestHeader for future considerations.
N 1.1
@nvl: Would such a function return the Verb or URL? I know `host` is a request header, but the verb and path go on a line before all named headers (from Fiddler: `GET /fiddler2/updatecheck.asp?isBeta=False HTTP/1.1`).
Andy E
@Andy: please refer to the url ('currently'). It will be considerde in future revisions. So specification is still not defined. It will probably contain all information that is in the request header.
N 1.1
@nvl: indeed, but I was assuming (rather safely, I think) a function labelled `getRequestHeader()` would take a single parameter for the name of the header (much like `getResponseHeader()`).
Andy E