ajax response is showing in console but not in alert box












0














I was creating a small game for childrens. The game is completed building but the problem am facing is the show the alert boxes when needed. Sometimes it shows sometimes it doesn't. When I pressed inspect element I found that its getting printed on the console but not coming in alert box. Sometimes when I hard reset the browser it shows the alert box when needed. I cleared the browser cache and tried its works sometimes and sometimes not. But its getting printed on the console properly as per my code.



Please someone help me on this issue. I have never faced these kind of issue, its very strange and am tensed.



Below is my jquery code:



<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>


Below is my php code:



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









share|improve this question
























  • Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
    – MrAleister
    Nov 20 '18 at 9:27










  • @MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
    – Snehasis
    Nov 20 '18 at 9:31










  • can you show the responce of ajax request?
    – Banujan Balendrakumar
    Nov 20 '18 at 9:38










  • Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
    – MrAleister
    Nov 20 '18 at 9:41






  • 1




    @BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
    – Snehasis
    Nov 20 '18 at 10:07
















0














I was creating a small game for childrens. The game is completed building but the problem am facing is the show the alert boxes when needed. Sometimes it shows sometimes it doesn't. When I pressed inspect element I found that its getting printed on the console but not coming in alert box. Sometimes when I hard reset the browser it shows the alert box when needed. I cleared the browser cache and tried its works sometimes and sometimes not. But its getting printed on the console properly as per my code.



Please someone help me on this issue. I have never faced these kind of issue, its very strange and am tensed.



Below is my jquery code:



<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>


Below is my php code:



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









share|improve this question
























  • Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
    – MrAleister
    Nov 20 '18 at 9:27










  • @MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
    – Snehasis
    Nov 20 '18 at 9:31










  • can you show the responce of ajax request?
    – Banujan Balendrakumar
    Nov 20 '18 at 9:38










  • Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
    – MrAleister
    Nov 20 '18 at 9:41






  • 1




    @BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
    – Snehasis
    Nov 20 '18 at 10:07














0












0








0







I was creating a small game for childrens. The game is completed building but the problem am facing is the show the alert boxes when needed. Sometimes it shows sometimes it doesn't. When I pressed inspect element I found that its getting printed on the console but not coming in alert box. Sometimes when I hard reset the browser it shows the alert box when needed. I cleared the browser cache and tried its works sometimes and sometimes not. But its getting printed on the console properly as per my code.



Please someone help me on this issue. I have never faced these kind of issue, its very strange and am tensed.



Below is my jquery code:



<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>


Below is my php code:



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









share|improve this question















I was creating a small game for childrens. The game is completed building but the problem am facing is the show the alert boxes when needed. Sometimes it shows sometimes it doesn't. When I pressed inspect element I found that its getting printed on the console but not coming in alert box. Sometimes when I hard reset the browser it shows the alert box when needed. I cleared the browser cache and tried its works sometimes and sometimes not. But its getting printed on the console properly as per my code.



Please someone help me on this issue. I have never faced these kind of issue, its very strange and am tensed.



Below is my jquery code:



<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>


Below is my php code:



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






javascript php jquery arrays ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:38









RiggsFolly

69.8k1864109




69.8k1864109










asked Nov 20 '18 at 9:16









Snehasis

480410




480410












  • Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
    – MrAleister
    Nov 20 '18 at 9:27










  • @MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
    – Snehasis
    Nov 20 '18 at 9:31










  • can you show the responce of ajax request?
    – Banujan Balendrakumar
    Nov 20 '18 at 9:38










  • Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
    – MrAleister
    Nov 20 '18 at 9:41






  • 1




    @BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
    – Snehasis
    Nov 20 '18 at 10:07


















  • Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
    – MrAleister
    Nov 20 '18 at 9:27










  • @MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
    – Snehasis
    Nov 20 '18 at 9:31










  • can you show the responce of ajax request?
    – Banujan Balendrakumar
    Nov 20 '18 at 9:38










  • Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
    – MrAleister
    Nov 20 '18 at 9:41






  • 1




    @BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
    – Snehasis
    Nov 20 '18 at 10:07
















Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
– MrAleister
Nov 20 '18 at 9:27




Just for testing purposes - replace sweetalert (swal) with native js alert and see if error persist.
– MrAleister
Nov 20 '18 at 9:27












@MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
– Snehasis
Nov 20 '18 at 9:31




@MrAleister yeah same happened with regular alert boxes also. I hard refreshed the browser it worked yesterday and again it showing the problem today. This time I cleared cache and hard refreshed the browser and still the problem persists. I used async:False in ajax but no success for me.
– Snehasis
Nov 20 '18 at 9:31












can you show the responce of ajax request?
– Banujan Balendrakumar
Nov 20 '18 at 9:38




can you show the responce of ajax request?
– Banujan Balendrakumar
Nov 20 '18 at 9:38












Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
– MrAleister
Nov 20 '18 at 9:41




Other thing is - your if statements relies on long/complex string comparison, thus are prone to typos. Try to simplify response and conditions (use codes like 'ALL_OK', 'ANIMAL_DEAD'), and keep long strings in alert code only.
– MrAleister
Nov 20 '18 at 9:41




1




1




@BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
– Snehasis
Nov 20 '18 at 10:07




@BanujanBalendrakumar Let me try to change the long sentence as MrAleistr said...if the problem still stays then I will share you my id and password for Anydesk or TeamViewer !
– Snehasis
Nov 20 '18 at 10:07












2 Answers
2






active

oldest

votes


















0














Please add this debug:



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

}





share|improve this answer





















  • success: 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(); }
    – Snehasis
    Nov 20 '18 at 12:45










  • I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
    – Snehasis
    Nov 20 '18 at 12:46












  • Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
    – MrAleister
    Nov 20 '18 at 12:47










  • As you can see I have used Print_r at last in my php page. that is printing this array
    – Snehasis
    Nov 20 '18 at 12:48






  • 1




    @Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
    – MrAleister
    Nov 20 '18 at 12:50



















-1














Try this:



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

}
});





share|improve this answer

















  • 1




    How can you say, This is the solution? Explain your code
    – Banujan Balendrakumar
    Nov 20 '18 at 9:53










  • @shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
    – Snehasis
    Nov 20 '18 at 9:57










  • async:false ? He is using success callback - why would you need async:false ?
    – MrAleister
    Nov 20 '18 at 9:57












  • when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
    – shaghayegh sheykholeslami
    Nov 20 '18 at 10:07












  • @shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
    – Snehasis
    Nov 20 '18 at 10:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389700%2fajax-response-is-showing-in-console-but-not-in-alert-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Please add this debug:



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

}





share|improve this answer





















  • success: 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(); }
    – Snehasis
    Nov 20 '18 at 12:45










  • I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
    – Snehasis
    Nov 20 '18 at 12:46












  • Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
    – MrAleister
    Nov 20 '18 at 12:47










  • As you can see I have used Print_r at last in my php page. that is printing this array
    – Snehasis
    Nov 20 '18 at 12:48






  • 1




    @Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
    – MrAleister
    Nov 20 '18 at 12:50
















0














Please add this debug:



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

}





share|improve this answer





















  • success: 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(); }
    – Snehasis
    Nov 20 '18 at 12:45










  • I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
    – Snehasis
    Nov 20 '18 at 12:46












  • Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
    – MrAleister
    Nov 20 '18 at 12:47










  • As you can see I have used Print_r at last in my php page. that is printing this array
    – Snehasis
    Nov 20 '18 at 12:48






  • 1




    @Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
    – MrAleister
    Nov 20 '18 at 12:50














0












0








0






Please add this debug:



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

}





share|improve this answer












Please add this debug:



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

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 12:35









MrAleister

439210




439210












  • success: 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(); }
    – Snehasis
    Nov 20 '18 at 12:45










  • I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
    – Snehasis
    Nov 20 '18 at 12:46












  • Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
    – MrAleister
    Nov 20 '18 at 12:47










  • As you can see I have used Print_r at last in my php page. that is printing this array
    – Snehasis
    Nov 20 '18 at 12:48






  • 1




    @Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
    – MrAleister
    Nov 20 '18 at 12:50


















  • success: 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(); }
    – Snehasis
    Nov 20 '18 at 12:45










  • I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
    – Snehasis
    Nov 20 '18 at 12:46












  • Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
    – MrAleister
    Nov 20 '18 at 12:47










  • As you can see I have used Print_r at last in my php page. that is printing this array
    – Snehasis
    Nov 20 '18 at 12:48






  • 1




    @Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
    – MrAleister
    Nov 20 '18 at 12:50
















success: 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(); }
– Snehasis
Nov 20 '18 at 12:45




success: 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(); }
– Snehasis
Nov 20 '18 at 12:45












I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
– Snehasis
Nov 20 '18 at 12:46






I have tried as you asked. and still the same error. "HARD_LUCK_DIED"Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) that is the above response I am getting in console
– Snehasis
Nov 20 '18 at 12:46














Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
– MrAleister
Nov 20 '18 at 12:47




Where are you getting this Array ( [Farmer] => 14 [Cow1] => 2 [Cow2] => 2 [Bunny1] => 1 [Bunny2] => 0 [Bunny3] => 6 [Bunny4] => 8 ) Array ( [0] => Farmer [1] => Cow1 [2] => Cow2 [3] => Bunny1 [5] => Bunny3 [6] => Bunny4 ) from ? console.log ?
– MrAleister
Nov 20 '18 at 12:47












As you can see I have used Print_r at last in my php page. that is printing this array
– Snehasis
Nov 20 '18 at 12:48




As you can see I have used Print_r at last in my php page. that is printing this array
– Snehasis
Nov 20 '18 at 12:48




1




1




@Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
– MrAleister
Nov 20 '18 at 12:50




@Snehasis - well, that's your bug. Remove that print_r - it's output is being added to http response
– MrAleister
Nov 20 '18 at 12:50













-1














Try this:



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

}
});





share|improve this answer

















  • 1




    How can you say, This is the solution? Explain your code
    – Banujan Balendrakumar
    Nov 20 '18 at 9:53










  • @shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
    – Snehasis
    Nov 20 '18 at 9:57










  • async:false ? He is using success callback - why would you need async:false ?
    – MrAleister
    Nov 20 '18 at 9:57












  • when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
    – shaghayegh sheykholeslami
    Nov 20 '18 at 10:07












  • @shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
    – Snehasis
    Nov 20 '18 at 10:12
















-1














Try this:



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

}
});





share|improve this answer

















  • 1




    How can you say, This is the solution? Explain your code
    – Banujan Balendrakumar
    Nov 20 '18 at 9:53










  • @shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
    – Snehasis
    Nov 20 '18 at 9:57










  • async:false ? He is using success callback - why would you need async:false ?
    – MrAleister
    Nov 20 '18 at 9:57












  • when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
    – shaghayegh sheykholeslami
    Nov 20 '18 at 10:07












  • @shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
    – Snehasis
    Nov 20 '18 at 10:12














-1












-1








-1






Try this:



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

}
});





share|improve this answer












Try this:



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

}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 9:47









shaghayegh sheykholeslami

1887




1887








  • 1




    How can you say, This is the solution? Explain your code
    – Banujan Balendrakumar
    Nov 20 '18 at 9:53










  • @shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
    – Snehasis
    Nov 20 '18 at 9:57










  • async:false ? He is using success callback - why would you need async:false ?
    – MrAleister
    Nov 20 '18 at 9:57












  • when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
    – shaghayegh sheykholeslami
    Nov 20 '18 at 10:07












  • @shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
    – Snehasis
    Nov 20 '18 at 10:12














  • 1




    How can you say, This is the solution? Explain your code
    – Banujan Balendrakumar
    Nov 20 '18 at 9:53










  • @shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
    – Snehasis
    Nov 20 '18 at 9:57










  • async:false ? He is using success callback - why would you need async:false ?
    – MrAleister
    Nov 20 '18 at 9:57












  • when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
    – shaghayegh sheykholeslami
    Nov 20 '18 at 10:07












  • @shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
    – Snehasis
    Nov 20 '18 at 10:12








1




1




How can you say, This is the solution? Explain your code
– Banujan Balendrakumar
Nov 20 '18 at 9:53




How can you say, This is the solution? Explain your code
– Banujan Balendrakumar
Nov 20 '18 at 9:53












@shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
– Snehasis
Nov 20 '18 at 9:57




@shaghayegh sheykholeslami: What you have changed ? its looks same as my code.
– Snehasis
Nov 20 '18 at 9:57












async:false ? He is using success callback - why would you need async:false ?
– MrAleister
Nov 20 '18 at 9:57






async:false ? He is using success callback - why would you need async:false ?
– MrAleister
Nov 20 '18 at 9:57














when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
– shaghayegh sheykholeslami
Nov 20 '18 at 10:07






when you use async:false your browser locked and waite for your response so for this reasone when you use console.log() or run your console you could see or run your success result.
– shaghayegh sheykholeslami
Nov 20 '18 at 10:07














@shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
– Snehasis
Nov 20 '18 at 10:12




@shaghayeghsheykholeslami I have used you code... But no positive result yet... :(
– Snehasis
Nov 20 '18 at 10:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389700%2fajax-response-is-showing-in-console-but-not-in-alert-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?