栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript:检测AJAX请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript:检测AJAX请求

以下是一些代码(通过粘贴到Chrome31.0.1650.63的控制台中进行测试),用于捕获和记录或以其他方式处理ajax请求及其响应:

(function() {    var proxied = window.XMLHttpRequest.prototype.send;    window.XMLHttpRequest.prototype.send = function() {        console.log( arguments );        //Here is where you can add any pre to process the request.         //If you want to pass the Ajax request object, pass the 'pointer' below        var pointer = this        var intervalId = window.setInterval(function(){     if(pointer.readyState != 4){  return;     }     console.log( pointer.responseText );     //Here is where you can add any pre to process the response.     //If you want to pass the Ajax request object, pass the 'pointer' below     clearInterval(intervalId);        }, 1);//I found a delay of 1 to be sufficient, modify it as you need.        return proxied.apply(this, [].slice.call(arguments));    };})();

此代码以可接受的答案解决了上述问题:

请注意,如果使用框架(如jQuery),则可能无法正常工作,因为调用send后它们可能会覆盖onreadystatechange(我认为jQuery确实如此)。或者他们可以覆盖send方法(但这是不可能的)。因此,这是部分解决方案。

因为它不依赖于未更改“ onreadystatechange”回调,而是监视“ readyState”本身。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号