基本上正如标题所说,我想从 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/