get 2 different url using xmlhttp.open

空扰寡人 提交于 2019-12-11 17:56:08

问题


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


回答1:


<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>



回答2:


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.



来源:https://stackoverflow.com/questions/10577213/get-2-different-url-using-xmlhttp-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!