ajax响应显示在控制台中,但不在警报框中

时间:2018-11-20 09:16:25

标签: javascript php jquery arrays 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);     
}
}

2 个答案:

答案 0 :(得分:0)

请添加此调试:

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

}

答案 1 :(得分:-1)

尝试一下:

$.ajax({
    url: 'perform_game.php',
    type: "POST",
    data: {'button_clicked': i},
    dataType: 'json',
    async:false

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

        }
});