Sha*_*har 5 javascript ajax xmlhttprequest
我一定会使用原生的javascript(虽然如果我将它转换为原生的javascript,jQuery解决方案也可以工作).
此外,我没有现有AJAX请求的句柄,所以我无法直接访问它们.
我正在寻找类似的东西:
var ajaxRequestsInProgress = document.getAllAjaxRunning();
ajaxRequestsInProgress[1].getStatus(); // will return the status of the request, for example
Run Code Online (Sandbox Code Playgroud)
所以我可以访问这个对象并检查/操作现有的ajax请求.
我们应该说,这有点棘手.没有本地方法可以做到这一点.所以我们需要做一些黑客攻击,修改本机XMLHttpRequest功能.像这样的东西:
var getAJAXRequests = (function() {
var oldSend = XMLHttpRequest.prototype.send,
currentRequests = [];
XMLHttpRequest.prototype.send = function() {
currentRequests.push(this); // add this request to the stack
oldSend.apply(this, arguments); // run the original function
// add an event listener to remove the object from the array
// when the request is complete
this.addEventListener('readystatechange', function() {
var idx;
if (this.readyState === XMLHttpRequest.DONE) {
idx = currentRequests.indexOf(this);
if (idx > -1) {
currentRequests.splice(idx, 1);
}
}
}, false);
};
return function() {
return currentRequests;
}
}());
Run Code Online (Sandbox Code Playgroud)
它可以调用getAJAXRequests().
你可以在jsFiddle上看到它.
| 归档时间: |
|
| 查看次数: |
3552 次 |
| 最近记录: |