Ajax success: {return false;}

I want to return false from $.ajax when success completed:

 $.ajax({ url: '' + $website_url + 'queries/voorraad_berekenen.php', type: 'post', data: { aantal: $(this).parent('form').children('.quantity').val(), item_number_1: $(this).parent('form').children('.item_number_1').val() }, success: function(result) { return false; } }); 

This does not work. Is there any work around?

+4
source share
4 answers

From your post, I assume that you are calling a function that contains $.ajax() , and try to return false to that function. but you cannot do this because AJAX is asynchronous.

It is better to call a function from the ajax success function, for example:

 $.ajax({ url: '' + $website_url + 'queries/voorraad_berekenen.php', type: 'post', data: { aantal: $(this).parent('form').children('.quantity').val(), item_number_1: $(this).parent('form').children('.item_number_1').val() }, success: function(result) { var returned = true; if(some_condition_not_satisfied) { returned = false; } else { } // call a function calledFromAjaxSuccess(returned); } }); function calledFromAjaxSuccess(result) { if(result) { alert('TRUE'); } else { alert('FALSE'); } } 
+10
source

Perhaps you could try something like this (values ​​1 and 0 should be changed to the ones you use):

 success: function(result){ if(result === '1'){ // do something } else return false; } 
+1
source

just use asunc false, it can work fine, I implemented only this

just try

 $.ajax({ url: '' + $website_url + 'queries/voorraad_berekenen.php', type: 'post', data: { aantal: $(this).parent('form').children('.quantity').val(), item_number_1: $(this).parent('form').children('.item_number_1').val() }, async: false, //=>>>>>>>>>>> here >>>>>>>>>>> success: function(result) { return false; } }); 

it works fine, try once

0
source

I am facing this problem. then i found the actual solution. use async: false, inside ajax call

 function thisisavailavailusername(uname){ var dataString = 'username='+ uname.value; var trueorfalse = false; $.ajax({ type: "post", url: "/BloodBook/checkavailableusername", data: dataString, cache: false, async : false, //user this then everything ok success: function(html){ if(html=="true"){ $('#validusername').html('Available'); trueorfalse = true; }else{ $('#validusername').html('Not Available'); trueorfalse = false; } }, error: function() { alert("Something Wrong..."); } }); return trueorfalse; 

}

0
source

Source: https://habr.com/ru/post/1414521/


All Articles