【问题标题】:what is the equivalent for ajaxComplete if I use a xhr request?如果我使用 xhr 请求,ajaxComplete 的等价物是什么?
【发布时间】:2015-02-16 21:59:25
【问题描述】:
我在一个应用程序中工作,我正在使用以下方式捕获所有 ajax 请求:
$(document).ajaxComplete(function (xhr, status) {
//...
});
现在,我正在使用 MicrosoftMvcAjax 和 MicrosoftAjax,当 XHR 请求完成时,控制台中会显示一条消息:XHR finished loading。
当xhr 请求完成或xhr 中的ajaxComplete 等价物时,我如何捕捉?
【问题讨论】:
标签:
javascript
jquery
asp.net-mvc-5
xmlhttprequest
microsoft-ajax
【解决方案1】:
使用计数器
var _complete = 0, _on = 0;
function incr(){ _on++; }
function comp(){ _complete++; if(_complete==_on) console.log("All the completed")}
// Now use these function wherever you want to make an AJAX Call like
incr();
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
comp();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();