Get request url from xhr object

前端 未结 6 1679
闹比i
闹比i 2020-12-03 04:31

Is there any way to extract the request url from an xhr object? I can see the url in firebug via the channel property but you cant query this using javascript.

6条回答
  •  暖寄归人
    2020-12-03 05:00

    With the following hack no wrapper is required:

    var xhrProto = XMLHttpRequest.prototype,
        origOpen = xhrProto.open;
    
    xhrProto.open = function (method, url) {
        this._url = url;
        return origOpen.apply(this, arguments);
    };
    

    Usage:

    var r = new XMLHttpRequest();
    r.open('GET', '...', true);
    alert(r._url); // opens an alert dialog with '...'
    

提交回复
热议问题