首页 > 解决方案 > 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);     
}
}

标签: javascriptphpjqueryarraysajax

解决方案


请添加此调试:

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();
   }

}

推荐阅读