定制XMLHtt prequest.prototype.open [英] custom XMLHttpRequest.prototype.open

查看:225
本文介绍了定制XMLHtt prequest.prototype.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  VAR开放= XMLHtt prequest.prototype.open;

XMLHtt prequest.prototype.open =函数(方法,URI,异步,用户通过){
    this.addEventListener(readystatechange功能(事件){
    如果(this.readyState == 4){
       无功自我=这一点;
       VAR响应= {
         方法:方法,
         URI:URI,
         responseText的:self.responseText
      };
      的console.log(响应);
    } 其他 {
        执行console.log(this.readyState);
    }
    }, 假);
  open.call(这一点,方法,URI,异步,用户通过);
};
 

我想被发送之前,他们对XHR听。同样的事情也给jQuery的 beforeSend $。阿贾克斯方法。

我的目标是被发送之前,他们所有XHR的听。我认为最接近将是检查上面是否 this.readyState === 1

将在code以上原因的任何AJAX库,比如jQuery的误动作,因为我使用的原型在 XMLHtt prequest

解决方案
  

我想被发送之前,他们对XHR听。

然后尝试欺骗发送()方法,而不是的open()之一。

  

请问code以上原因的任何AJAX库,比如jQuery的误动作,因为我使用的原型上XMLHtt prequest?

没有,真的没有。只是,

  • 在这是行不通的,其中这些库选择不使用 XMLHtt prequest (尤其是IE)
  • ...甚至失败,如果浏览器不支持 XMLHtt prequest 对象(或不支持访问或修改其原型)
  • 在库也许能之前,你可以解决你的恶搞提领的功能(虽然我不知道是否有任何共同的lib)
  • 您$​​ C $ C调用与参数的定数,不知道这会影响任何东西,它不会再返回结果(即使我们知道这是未定义)。要100%地肯定,使用返回open.apply(这一点,参数);

var open = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function(method, uri, async, user, pass) {
    this.addEventListener("readystatechange", function(event) {  
    if(this.readyState == 4){
       var self = this;
       var response = {
         method: method,
         uri: uri,
         responseText: self.responseText
      };
      console.log(response);  
    } else {
        console.log(this.readyState);
    }
    }, false);
  open.call(this, method, uri, async, user, pass);
};

I am trying to listen for XHR before they are being sent. Something similar to jQuery's beforeSend in the $.ajax method.

My goal is to listen for all XHR's before they are being sent. I suppose the closest thing would be to check above if this.readyState === 1?

Would the code above cause any ajax libraries like jQuery to malfunction because I use prototype on XMLHttpRequest?

解决方案

I am trying to listen for XHR before they are being sent.

Then try to spoof the send() method, not the open() one.

Would the code above cause any ajax libraries like jQuery to malfunction because I use prototype on XMLHttpRequest?

No, not really. Only,

  • it won't work where those libs choose not to use XMLHttpRequest (particularly IE)
  • …and even fail if the browser does not support the XMLHttpRequest object (or does not support accessing or modifying its prototype)
  • libs might be able to work around your spoof by dereferencing the functions before you can (though I don't know any common lib that does)
  • Your code calls the native method with a fixed number of arguments, not sure if that affects anything, and it does not re-return the result (even if we know it's undefined). To be 100% sure, use return open.apply(this, arguments);.

这篇关于定制XMLHtt prequest.prototype.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆