gpt4 book ai didi

javascript - 响应值在 ajax 中重叠

转载 作者:行者123 更新时间:2023-11-30 15:29:34 25 4
gpt4 key购买 nike

我正在制作一个需要多个 Ajax 请求的应用程序。但问题是我得到的两个 Ajax 请求的响应值相同。在每个请求中都需要有一个名为 activityCode 的数据,但这样做我一直在获取 ScoreBoardResponse 的值,即使在 heartbeat 函数 中也是如此。如果我将 activityCode 中的任何一个重命名为任何其他名称,问题就会得到解决。但是为什么会这样呢?

下面是代码:

JS

var allJoined = false;
var roomName = $('#room').val();
var playerNameSet = function () {
if(!allJoined) {
$.ajax({
type: "POST",
url: "gameEngine/app.php",
data: {
activityCode: 1,
room: roomName
},
success: function (ScoreBoardResponse) {
var obj = JSON.parse(ScoreBoardResponse);
var count = Object.keys(obj).length;
if (count == 1) {
playerOne_name.html(obj.p1_name);
setTimeout(playerNameSet, 3000);
}
else if (count == 2) {
playerOne_name.html(obj.p1_name);
playerTwo_name.html(obj.p2_name);
allJoined = true;
//Start the heartbeat to check if the other player is alive
setTimeout(startHeartbeat, 15000);
clearTimeout(playerNameSet);
}
},
error: function (error) {
console.log(error);
}
});
}
};
setTimeout(playerNameSet, 3000);

function startHeartbeat() {
$.ajax({
type: "POST",
url: "gameEngine/app.php",
data: {
activityCode: 2,
room: roomName
},
success: function(beat) {
console.log(beat);
},
error: function(error) {
console.log(error);
}
});
setTimeout(startHeartbeat, 15000);
}

PHP

.
.
.
elseif (isset($_POST['activityCode']) == 1 && isset($_POST['room'])) {
$response = $gameHandler->getPlayerOrder($_POST['room']);

echo $response;
}
elseif (isset($_POST['activityCode']) == 2 && isset($_POST['room'])) {
echo "request reached here";
}

最佳答案

isset()如果 POST 变量存在或不存在,则返回 truefalse。它不返回变量值,您需要在您的条件中添加另一个检查:

elseif (isset($_POST['activityCode']) && $_POST['activityCode'] == 1 && isset($_POST['room'])) {
$response = $gameHandler->getPlayerOrder($_POST['room']);

echo $response;
}
elseif (isset($_POST['activityCode']) && $_POST['activityCode'] == 2 && isset($_POST['room'])) {
echo "request reached here";
}

关于javascript - 响应值在 ajax 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42439217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com