Question

I am new to Ajax. I wrote the following but cannot seem to figure out what's wrong with it. I am trying to invoke multiple HTTP Request calls to the web server. I looked at examples online and came up with the following.

Well, my code is not working because of some issue. Can anyone help me fix this code?

function getCurBid(bidID){
   var XMLHttpRequestObject = false;

   if (window.XMLHttpRequest)
   {
       // code for IE7+, Firefox, Chrome, Opera, Safari
       XMLHttpRequestObject = new XMLHttpRequest();
   }
   else if (window.ActiveXobject) 
   {
       // code for IE6, IE5
       XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if(XMLHttpRequestObject)
   { 
       var objID = document.getElementById("curBid"+bidID); 

       XMLHttpRequestObject.open("GET","ajax_getBid.cfm?ida="+bidID,true);


       if (XMLHttpRequestObject.readyState == 4 && 
           XMLHttpRequestObject.status == 200) 
       { 
           objID.innerHTML = XMLHttpRequestObject.responseText; 
       } 

       XMLHttpRequestObject.send(); 
   }
}
Was it helpful?

Solution

you need something more like this:

XMLHttpRequestObject.onreadystatechange = function() {
    if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
           objID.innerHTML = XMLHttpRequestObject.responseText; 
    }
}

you need to wrap your logic in the event handler

So integrated with your script would be:

function getCurBid(bidID){
   var XMLHttpRequestObject = false;

   if (window.XMLHttpRequest)
   {
       // code for IE7+, Firefox, Chrome, Opera, Safari
       XMLHttpRequestObject = new XMLHttpRequest();
   }
   else if (window.ActiveXobject) 
   {
       // code for IE6, IE5
       XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
   }

   if(XMLHttpRequestObject)
   { 
       var objID = document.getElementById("curBid"+bidID); 

       XMLHttpRequestObject.open("GET","ajax_getBid.cfm?ida="+bidID,true);

       XMLHttpRequestObject.onreadystatechange = function() {
           if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
               objID.innerHTML = XMLHttpRequestObject.responseText; 
           } 
       };

       XMLHttpRequestObject.send(); 
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top