get 2 different url using xmlhttp.open

2.2k views Asked by At

Need help on this following code. the problem is it only trigger the last xmlhttp.open not both. code as below.

<script type="text/javascript">
function loadXMLDoc(obj){ 
  var xmlhttp; 
  var flag; 
  var mycode;
  mycode = obj;

  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){
    }
  }
   flag = document.getElementById(mycode);
   if (flag.value == "Uncall") {
     flag.value = "Call Patient";
     document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Called</font></b>";        
   }else{ 
     flag.value = "Uncall";
     document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Calling..</font></b>";
     **xmlhttp.open("GET","http://1.1.1.1/callnumber.cgi?"+mycode,true);
     xmlhttp.send();
     xmlhttp.open("GET","http://2.2.2.2/displaynumber.php?"+mycode,true);
     xmlhttp.send();**
   }
}
</script>

appreciate some input

2

There are 2 answers

5
Ciro On BEST ANSWER
<script type="text/javascript">
function loadXMLDoc(obj){
  var xmlhttp; 
  var flag;
  var mycode;
  mycode = obj;
  flag = document.getElementById(mycode);
  if (flag.value != "Uncall"){
      loadXMLDocCall(obj,"http://1.1.1.1/callnumber.cgi?");
      loadXMLDocCall(obj,"http://2.2.2.2/displaynumber.php?");
      flag.value = "Uncall";
      document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Calling..</font></b>";
      } else { 
       flag.value = "Call Patient";
       document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Called</font></b>";
      }
}
function loadXMLDocCall(obj,url){ 
  var xmlhttp; 
  var mycode;
  mycode = obj;

  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){
    }
  }
   xmlhttp.open("GET",url+mycode,true);
   xmlhttp.send();
 }
</script>
1
Programming Guy On

Don't reuse the same request object.

Best advice is don't write any of this yourself. Find a library and use their implementation. JQuery isn't bad. There are plenty of other options too.