javascript - 从 XHR 获取方法和 url

标签 javascript xmlhttprequest

基本上正如标题所说,我想从 xhr 获取 URL 和 HTTP 动词。这可能吗?

最佳答案

恐怕不是原生的。在可原型(prototype)化的实现中,您可以编写自己的原型(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); 
}

但不要指望它能在 IE7 及更早版本中运行。


我想您可以通过完全重新创建 XMLHttpRequest 对象来做到这一点,但是需要做很多工作才能做到正确:

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

当然,你必须花功夫正确绑定(bind)onreadystatechange和设置status等。

关于javascript - 从 XHR 获取方法和 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2402007/

相关文章:

javascript - 使用 javascript 添加新的 <li> 到 <ul> onclick 时遇到问题

javascript - 谷歌折线图将 float 转换为整数

javascript - 在 Javascript 中获取互联网连接状态

javascript - 传出数据的 XmlHttpRequest overrideMimeType

Android WebView XMLHttpRequest Keepalive

javascript - 流类型和 DOM 对象

javascript - 在新窗口中打开链接,然后立即显示打印窗口

javascript - $q 推迟在 Jasmine 中不触发延续

firefox - Firefox 更新后的中文跨域 XmlHTTPRequest

javascript - 检查 XHR2 文件上传支持的最佳方法是什么?