简体   繁体   English

从xhr对象获取请求URL

[英]Get request url from xhr object

Is there any way to extract the request url from an xhr object? 有没有办法从xhr对象中提取请求URL? I can see the url in firebug via the channel property but you cant query this using javascript. 我可以通过channel属性看到firebug中的url,但是你无法使用javascript查询它。

If you are using jQuery, you can make use of the "beforeSend" function in the AJAX request to modify the jqXHR object. 如果您使用的是jQuery,则可以使用AJAX请求中的“beforeSend”函数来修改jqXHR对象。 Ie, 也就是说,

$.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. 传递给各种回调的jqXHR对象将具有您可以访问的变量jqXHR.requestURL

With the following hack no wrapper is required: 通过以下hack,不需要包装器:

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 '...'

I hope I'm understanding your problem correctly. 我希望我能正确理解你的问题。

It should be fairly simple to wrap your XHR objects with your own object to allow for this kind of functionality. 使用您自己的对象包装XHR对象应该相当简单,以允许这种功能。

Below is an overly simplified example: 以下是一个过于简化的例子:

// Assumption: GetXHR() returns a new XHR object, cross browser.

function HTTPGet(url, onStartCb, onCompleteCb)
{
  var xhr = GetXHR();
  // Construct your own xhr object that contains the url and the xhr object itself.
  var myXhr = { xhr: xhr, url: url };

  xhr.onreadystatechange = function()
  {
     if (xhr.readyState == 4 && xhr.status == 200)
     {
        onCompleteCb(myXhr);
     }
  };

  xhr.open("GET", url);
  onStartCb(myXhr);
  xhr.send(null);
}

I haven't tested this extensively, but it should work and with some modifications (error handling, passing parameters, etc) you should probably be able to turn this example into a fully functional solution. 我没有对此进行过广泛的测试,但它应该可以工作并进行一些修改(错误处理,传递参数等),您应该可以将此示例转换为功能完备的解决方案。

According to https://developer.mozilla.org/en/XmlHttpRequest , you can indeed get channel if you have elevated privileges; 根据https://developer.mozilla.org/en/XmlHttpRequest ,如果你拥有提升的权限,你确实可以获得channel ; however, channel is non-standard (might not work in other browsers), and indeed the W3C specs at http://www.w3.org/TR/XMLHttpRequest/ do not mention channel nor any other way to "extract the request URL". 但是, channel是非标准的(可能在其他浏览器中不起作用),实际上http://www.w3.org/TR/XMLHttpRequest/上的W3C规范没有提到channel也没有任何其他方式来“提取请求URL” ”。 I suspect, therefore, that there is no way to do so reasonably across browsers. 因此,我怀疑在浏览器中没有办法合理地这样做。

A couple of related properties that could be useful are: 一些可能有用的相关属性是:


xhr2 XHR2

XMLHttpRequest.responseURL - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseURL . XMLHttpRequest.responseURL - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseURL

This is not supported by IE. IE不支持此功能。


fetch

If you are using fetch you can use Response.url - https://developer.mozilla.org/en-US/docs/Web/API/Response/url . 如果您使用的是fetch ,则可以使用Response.url - https://developer.mozilla.org/en-US/docs/Web/API/Response/url

This is not supported by IE. IE不支持此功能。

I also needed this because I was executing a for loop where I did URL requests. 我也需要这个,因为我正在执行一个for循环,我在那里做了URL请求。

Since usually you have the URL request before you call the open method on the XMLHttpRequest object, you can assign a random property on the xml object (in my case, the property url): 由于通常在调用XMLHttpRequest对象上的open方法之前有URL请求,因此可以在xml对象上分配一个随机属性(在我的例子中,属性url):

var xml = new XMLHttpRequest();
xml.onreadystatechange = function() {
    if ( xml.readyState == 4) {
        console.log(xml.url); // equal to originalUrl
    }
};
xml.open("GET", originalUrl, true);
xml.url = originalUrl;
xml.send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM