javascriptjqueryasp.net-mvc-5xmlhttprequestmicrosoft-ajax

what is the equivalent for ajaxComplete if I use a xhr request?


I'm working in an app and I was catching all the ajax request using:

$(document).ajaxComplete(function (xhr, status) {
        //...
});

Now, I'm using MicrosoftMvcAjax and MicrosoftAjax and when a XHR request finished a message in the console says: XHR finished loading.

How I can catch when a xhr request finish or what is the equivalent for ajaxComplete in xhr?


Solution

  • Use a counters

    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();