Get request url from xhr object

前端 未结 6 1675
闹比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 04:48

    If you are using jQuery, you can make use of the "beforeSend" function in the AJAX request to modify the jqXHR object. I.e.,

    $.ajax({
    ...
    url: "http://some/url",
    beforeSend: function(jqxhr, settings) { jqxhr.requestURL = "http://some/url"; },
    ...
    });
    

    The jqXHR object passed to the various callbacks will then have that variable, jqXHR.requestURL, which you can access.

提交回复
热议问题