ajax response is showing in console but not in alert box
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
|
show 15 more comments
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
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
|
show 15 more comments
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
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
javascript php jquery arrays ajax
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
|
show 15 more comments
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
|
show 15 more comments
2 Answers
2
active
oldest
votes
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();
}
}
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 thisArray ( [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
|
show 3 more comments
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();
}
}
});
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 usingsuccess
callback - why would you needasync: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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
}
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 thisArray ( [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
|
show 3 more comments
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();
}
}
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 thisArray ( [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
|
show 3 more comments
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();
}
}
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();
}
}
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 thisArray ( [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
|
show 3 more comments
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 thisArray ( [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
|
show 3 more comments
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();
}
}
});
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 usingsuccess
callback - why would you needasync: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
|
show 1 more comment
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();
}
}
});
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 usingsuccess
callback - why would you needasync: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
|
show 1 more comment
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();
}
}
});
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();
}
}
});
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 usingsuccess
callback - why would you needasync: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
|
show 1 more comment
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 usingsuccess
callback - why would you needasync: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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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