代码之家  ›  专栏  ›  技术社区  ›  Pablo Fernandez

从xhr获取方法和url

  •  2
  • Pablo Fernandez  · 技术社区  · 16 年前

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

    2 回复  |  直到 16 年前
        1
  •  2
  •   Andy E    16 年前

    恐怕不是本地的。在原型实现中,您可以编写自己的原型:

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

    当然,您必须努力正确绑定 onreadystatechange 设置 status 等。

        2
  •  1
  •   N 1.1    16 年前
    Currently ,没有 标准 从xhr对象获取http动词或url的方法。但是,w3c正在考虑 getRequestHeader 为了将来的考虑。