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
?
open() The XMLHttpRequest method open() initializes a newly-created request, or re-initializes an existing one. Note: Calling this method for an already active request (one for which open() has already been called) is the equivalent of calling abort() .
You can get it by XMLHttpRequest. responseText in XMLHttpRequest. onreadystatechange when XMLHttpRequest. readyState equals to XMLHttpRequest.
The read-only XMLHttpRequest. responseURL property returns the serialized URL of the response or the empty string if the URL is null . If the URL is returned, any URL fragment present in the URL will be stripped away. The value of responseURL will be the final URL obtained after any redirects.
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,
XMLHttpRequest
(particularly IE)XMLHttpRequest
object (or does not support accessing or modifying its prototype)undefined
). To be 100% sure, use return open.apply(this, arguments);
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With