我正在尝试通过JS检测ajax响应,并且在此SO帖子JavaScript: Detect AJAX requests上找到了以下代码
问题是我收到两个响应,并且触发另一个函数运行2x。
是否可以遍历响应并仅发送第一个响应,而不同时发送两个响应?某种IndexOf 0?
(function () {
var proxied = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function () {
// console.log( arguments );
var pointer = this
var intervalId = window.setInterval(function () {
if (pointer.readyState != 4) {
return;
}
var data = pointer.responseText;
var jsonResponse = JSON.parse(data);
console.log('json response: ',jsonResponse);
clearInterval(intervalId);
},1);
return proxied.apply(this,[].slice.call(arguments));
};
})();