微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Ajax请求只提交最后一个

我在一个名为’inject.PHP’的文件中有这个代码,我用它在游戏中检索更新多个值,但请求只获取最后一个值“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的值

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐