Ajax请求只提交最后一个


Ajax request only submits last one

我在一个名为"inject.php"的文件中有这段代码,我用它来检索游戏中的多个值的更新,但请求只获得最后一个值"health",并在所有其他feild:/中显示health

 < script type = "text/javascript" >
   function getAttack() {
     if (window.XMLHttpRequest) {
       // Create the object for browsers
       xmlhttp = new XMLHttpRequest();
     } else {
       // Create the object for browser versions prior to IE 7
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function() {
         // if server is ready with the response
         if (xmlhttp.readyState == 4) {
           // if everything is Ok on browser
           if (xmlhttp.status == 200) {
             //Update the div with the response
             document.getElementById("attack").innerHTML = xmlhttp.responseText;
           }
         }
       }
       //send the selected option id to the php page 
     xmlhttp.open("GET", "ajax/fetchAttack.php", true);
     xmlhttp.send();
   }
 function getDefense() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("defense").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchDefense.php", true);
   xmlhttp.send();
 }
 function getMoney() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("money").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchMoney.php", true);
   xmlhttp.send();
 }
 function getRank() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("rank").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchRank.php", true);
   xmlhttp.send();
 }
 function getExp() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("exp").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchExp.php", true);
   xmlhttp.send();
 }
 function getLevel() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("level").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchLevel.php", true);
   xmlhttp.send();
 }
 function getHealth() {
   if (window.XMLHttpRequest) {
     // Create the object for browsers
     xmlhttp = new XMLHttpRequest();
   } else {
     // Create the object for browser versions prior to IE 7
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
       // if server is ready with the response
       if (xmlhttp.readyState == 4) {
         // if everything is Ok on browser
         if (xmlhttp.status == 200) {
           //Update the div with the response
           document.getElementById("health").innerHTML = xmlhttp.responseText;
         }
       }
     }
     //send the selected option id to the php page 
   xmlhttp.open("GET", "ajax/fetchHealth.php", true);
   xmlhttp.send();
 }
 setInterval(getAttack, 1000);
 setInterval(getDefense, 1000);
 setInterval(getMoney, 1000);
 setInterval(getRank, 1000);
 setInterval(getExp, 1000);
 setInterval(getLevel, 1000);
 setInterval(getHealth, 1000);
 < /script>

添加

var xmlhttp;

到每个功能的顶部

目前您使用的是全局xmlhttp变量,因此每个函数都会破坏xmlhttp

的值

由于不使用var xmlhttp;,它将成为一个全局变量,并被每个函数覆盖。因此,最后一个函数是将AJAX调用的URL设置为"ajax/fetchHealth.php"。因此,基本上所有的功能都是将请求发送到相同的URL,并得到相同的响应。希望这能有所帮助。