multiple ajax calls simultaneously

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Early Woods
    New Member
    • Mar 2011
    • 2

    multiple ajax calls simultaneously

    Hey,

    In the following code the function showdivs() is called on body load, though only the second div is loaded. I've been searching on the web for similar problems and I know it has to do with the handler function, which is supposed to be run twice but now just cancels the first function as soon as the second one is called. Could anybody explain me how to solve this?
    TIA,

    Woods

    Code:
    var time_variable;
     
    function getXMLObject()  //XML OBJECT
    {
       var xobject = false;
       try {
         xobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
       }
       catch (e) {
         try {
           xobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
         }
         catch (e2) {
           xobject = false   // No Browser accepts the XMLHTTP Object then false
         }
       }
       if (!xobject && typeof XMLHttpRequest != 'undefined') {
         xobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
       }
       return xobject;  // Mandatory Statement returning the ajax object created
    }
     
    var xobject = new getXMLObject();	//xmlhttp holds the ajax object
    
    
    function handler() {
    	var getdate = new Date();
    	
    	if(xobject) {
    		xobject.open("POST",phpFile,true);
    		xobject.onreadystatechange  = function() {
    			if (xobject.readyState==4) {
    				if(xobject.status == 200) {
    					document.getElementById(divID).innerHTML=xobject.responseText;
    				}
    				else {
    					alert('error');
    				}
    			}
    		}
    		xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    		xobject.send(postVars);
    	} 
    }
    
    
    function showdiv1() {
      	divID = "div1"; //define html div
      	phpFile = "showdiv1.php"; //define file to be loaded
      	postVars = "null"; //define post vars
      	new handler();
    }
    
    function showdiv2() {
      	divID = "div2"; //define html div
      	phpFile = "showdiv2.php"; //define file to be loaded
      	postVars = "null"; //define post vars
      	new handler();
    }
    
    function showdivs() {
    showdiv1();
    showdiv2();
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    actually, both are loaded. it’s just that the second overwrites the first one.

    PS. it’s null, not "null"

    Comment

    • Early Woods
      New Member
      • Mar 2011
      • 2

      #3
      Solved it - suppose nice to share?

      Code:
      var time_variable;
       
      function getXMLObject()  //XML OBJECT
      {
         var gobject = false;
         try {
           gobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
         }
         catch (e) {
           try {
             gobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
           }
           catch (e2) {
             gobject = false   // No Browser accepts the XMLHTTP Object then false
           }
         }
         if (!gobject && typeof XMLHttpRequest != 'undefined') {
           gobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
         }
         return gobject;  // Mandatory Statement returning the ajax object created
      }
      
      function handler(div) {
      
      	var xobject = new getXMLObject();    //xmlhttp holds the ajax object
      	var randint = Math.random();
      	var getdate = new Date();
      		
      		xobject.open("POST",phpFile,true);
      		xobject.onreadystatechange  = function() {
      			if (xobject.readyState==4) {
      				if(xobject.status == 200) {
      					document.getElementById(div).innerHTML=xobject.responseText;
      				}
      				else {
      					alert('error');
      				}
      			}
      		}
      		xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      		xobject.send(postVars);
      	} 
      
      
      function showdiv1() {
        	divID = "div1"; //define html div
        	phpFile = "showdiv1.php"; //define file to be loaded
        	postVars = "var1=somevalue"; //define post vars
        	handler(divID);
      }
      
      function showdiv2() {
        	divID = "div2"; //define html div
        	phpFile = "showdiv2.php"; //define file to be loaded
        	postVars = "var2=somevalue"; //define post vars
        	handler(divID);
      }
      
      function showdiv3() {
        	divID = "div3"; //define html div
        	phpFile = "showdiv3.php"; //define file to be loaded
        	postVars = "verzend=somevalue"; //define post vars
        	handler(divID);
      }
      
      function showdivs() {
      showdiv1();
      showdiv2();
      showdiv3();
      }

      Comment

      Working...