【问题标题】:ajax response is showing in console but not in alert boxajax 响应显示在控制台中,但未显示在警报框中
【发布时间】:2018-11-20 09:16:25
【问题描述】:

我正在为儿童制作一个小游戏。游戏已完成构建,但面临的问题是在需要时显示警报框。有时显示有时不显示。当我按下检查元素时,我发现它打印在控制台上但没有出现在警报框中。有时当我硬重置浏览器时,它会在需要时显示警告框。我清除了浏览器缓存并有时尝试其工作,有时不尝试。但它会根据我的代码正确打印在控制台上。

请有人帮助我解决这个问题。我从来没有遇到过这种问题,很奇怪,很紧张。

下面是我的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);     
}
}

【问题讨论】:

  • 仅用于测试目的 - 将 sweetalert (swal) 替换为原生 js alert 并查看错误是否仍然存在。
  • @MrAleister 是的,常规警报框也发生了同样的情况。我努力刷新了昨天工作的浏览器,今天又显示了问题。这次我清除了缓存并硬刷新了浏览器,但问题仍然存在。我在 ajax 中使用了 async:False 但对我来说没有成功。
  • 可以显示ajax请求的响应吗?
  • 另一件事是 - 您的 if 语句依赖于长/复杂的字符串比较,因此容易出现拼写错误。尝试简化响应和条件(使用“ALL_OK”、“ANIMAL_DEAD”等代码),并仅在警报代码中保留长字符串。
  • @BanujanBalendrakumar 让我尝试更改 MrAleistr 所说的长句...如果问题仍然存在,那么我将分享我的 Anydesk 或 TeamViewer 的 ID 和密码!

标签: javascript php jquery arrays ajax


【解决方案1】:

请添加此调试:

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

}

【讨论】:

  • 成功:function(data) { console.log('data is: ',data); console.log('data == MAX_CLICK_LOSE',data=="MAX_CLICK_LOSE"); if(data == "MAX_CLICK_LOSE") { swal({title: "Game Over !", text: "Maximum Clicks Reached. You Lose !", type: "error"}); $("#feed_btn").hide(); $("#finish_btn").show(); }
  • 我已经按照您的要求进行了尝试。仍然是同样的错误。 “HARD_LUCK_DIED”数组([农夫] => 14 [牛1] => 2 [牛2] => 2 [兔子1] => 1 [兔子2] => 0 [兔子3] => 6 [兔子4] => 8)数组( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) 这是我在控制台中得到的上述响应
  • 你从哪里得到这个Array ( [Farmer] =&gt; 14 [Cow1] =&gt; 2 [Cow2] =&gt; 2 [Bunny1] =&gt; 1 [Bunny2] =&gt; 0 [Bunny3] =&gt; 6 [Bunny4] =&gt; 8 ) Array ( [0] =&gt; Farmer [1] =&gt; Cow1 [2] =&gt; Cow2 [3] =&gt; Bunny1 [5] =&gt; Bunny3 [6] =&gt; Bunny4 )?控制台日志?
  • 如你所见,我终于在我的 php 页面中使用了 Print_r。正在打印这个数组
  • @Snehasis - 好吧,那是你的错误。删除那个 print_r - 它的输出被添加到 http 响应中
【解决方案2】:

试试这个:

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

        }
});

【讨论】:

  • 你怎么说,这就是解决方案?解释你的代码
  • @shaghayegh sheykholeslami:你改变了什么?它看起来和我的代码一样。
  • async:false ?他正在使用success 回调 - 为什么需要async:false
  • 当您使用 async:false 时,您的浏览器被锁定并等待您的响应,因此当您使用 console.log() 或运行控制台时,您可以看到或运行您的成功结果。
  • @shaghayeghsheykholeslami 我用过你的代码...但还没有积极的结果... :(
猜你喜欢
  • 1970-01-01
  • 2020-06-28
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多