从XMLHttpRequest中获取请求的URL


在编写Ajax通用错误处理程序时,经常需要记录发生错误的XMLHttpRequest的请求URL。但查询文档,并未找到从XMLHttpRequest中获取请求URL的方法。

javascript - Get request url from xhr object - Stack Overflow中提供了一种可能的实现方式:为浏览器原生的XMLHttpRequest包上一层。实现代码如下:

var xhrProto = XMLHttpRequest.prototype,
    origOpen = xhrProto.open;

xhrProto.open = function (method, url) {
    this._url = url;
    return origOpen.apply(this, arguments);
};

使用示例:

var r = new XMLHttpRequest();
r.open('GET', '...', true);
alert(r._url); // opens an alert dialog with '...'


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM