我将此代码放在名为' 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>
&#13;
答案 0 :(得分:8)
添加
var xmlhttp;
到每个函数的顶部
目前您正在使用全局xmlhttp
变量,因此每个函数都会破坏xmlhttp的值
答案 1 :(得分:0)
当你没有使用var xmlhttp;
时,它正在变成一个全局变量并被每个函数覆盖。所以最后一个函数是将AJAX调用的URL设置为"ajax/fetchHealth.php"
。所以基本上所有的功能都是将请求发送到同一个URL并获得相同的响应。希望这会有所帮助。