javascript - ajax 响应显示在控制台中,但未显示在警报框中
问题描述
我正在为儿童制作一个小游戏。游戏已完成构建,但面临的问题是在需要时显示警报框。有时显示有时不显示。当我按下检查元素时,我发现它打印在控制台上但没有出现在警报框中。有时当我硬重置浏览器时,它会在需要时显示警告框。我清除了浏览器缓存并有时尝试其工作,有时不尝试。但它会根据我的代码正确打印在控制台上。
请有人在这个问题上帮助我。我从来没有遇到过这样的问题,很奇怪,很紧张。
下面是我的jQuery代码:
<script>
$(document).ready(function(){
$("#feed_btn").hide();
$("#finish_btn").hide();
$("#start_btn").click(function(){
$("#start_btn").hide();
$("#feed_btn").show();
});
});
function RestartGame()
{
location.reload();
}
var i=0;
function PerformGame()
{
i++;
$.ajax({
url: 'perform_game.php',
type: "POST",
data: {'button_clicked': i},
dataType: 'json',
success: function(data) {
if(data == "Maximum Clicks Reached. You Lose !") {
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
if(data == "Maximum Clicks Reached. You Won !") {
swal({title: "Wow !",text: data,type: "success"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
if(data == "Hard Luck ! One or Some animal(s) died.") {
swal({title: "Dead !",text: data,type: "warning"});
}
if(data == "Farmer Died ! You Lose !"){
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
}
});
}
</script>
下面是我的php代码:
foreach($_SESSION['animal_life'] as $key => $value)
{
if ($value == '0') {
if ($key == 'Farmer'){
$msg = "Farmer Died ! You Lose !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
break;
}
$search_animal = array_search($key, $_SESSION['animals']);
unset($_SESSION['animals'][$search_animal]);
}
$check_animals_left = count($_SESSION['animals']);
if($check_animals_left < $count_total_animals) {
$msg = "Hard Luck ! One or Some animal(s) died.";
}
}
if ($no_of_times_button_clicked == '50') {
$counter = 0;
if (in_array('Farmer', $_SESSION['animals'])) {
$counter++;
}
if (in_array('Cow1', $_SESSION['animals']) || in_array('Cow2', $_SESSION['animals'])) {
$counter++;
}
if (in_array('Bunny1', $_SESSION['animals']) || in_array('Bunny2', $_SESSION['animals']) || in_array('Bunny3', $_SESSION['animals']) || in_array('Bunny4', $_SESSION['animals']))
{
$counter++;
}
if ($counter == 3)
{
$msg = "Maximum Clicks Reached. You Won !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
} else {
$msg = "Maximum Clicks Reached. You Lose !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
}
}
if ($msg) {
echo json_encode($msg);
}
}
解决方案
请添加此调试:
console.log('data is: ',data)
console.log('data == Maximum Clicks Reached. You Lose !',data == "Maximum Clicks Reached. You Lose !")
if(data == "Maximum Clicks Reached. You Lose !") {
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
console.log('data == Maximum Clicks Reached. You Won',data == "Maximum Clicks Reached. You Won !")
if(data == "Maximum Clicks Reached. You Won !") {
swal({title: "Wow !",text: data,type: "success"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
console.log('data == Hard Luck ! One or Some animal(s) died.',data == "Hard Luck ! One or Some animal(s) died.")
if(data == "Hard Luck ! One or Some animal(s) died.") {
swal({title: "Dead !",text: data,type: "warning"});
}
console.log('data == Farmer Died ! You Lose !',data == "Farmer Died ! You Lose !")
if(data == "Farmer Died ! You Lose !"){
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
}
推荐阅读
- python - 神经网络训练功能的“错误:列表分配索引超出范围”
- java - 计算列表中对象的出现次数
- swift - 如何将 NSAttributedString 拆分为 NSAttributedString 数组
- c# - Linq lamda EXISTS() 查找具有特定属性的对象
- javascript - flatrepo(monorepo 混合)webpack 找不到模块:'Invariant' 安装不变量没有影响
- mysql - 计算所有具有特定金额总和的用户
- python - 如果单击了按钮,如何跟踪
- javascript - 使用本地存储站点隐藏模式不起作用
- json - 将 AWS 云形成模板内容转换为 java 对象
- python - 池中的 Seaborn/Matplotlib - X 服务器“:1”上的致命 IO 错误 11(资源暂时不可用)