I want to return false
from $.ajax
when success
is complete:
$.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 doesn't work. Is there a work around?
Another possibility (which is effectively the same thing) is to call a function inside your success: callback that passes the data when it is available. You CAN return a value from an AJAX request, but that's only if you make it non-asynchronous (set the option async: false).
it always returns undefined to the var response variable as it is asynchronous call to ajax so the invoking code continues it execution. Kindly advise anybody. Thanks in advance. I'm not familiar with returnData.then () so I have no idea if that is actually doing anything.
You can't call the UserAuthorityCheck () function and wait for a return value. You have to move the code after the call to UserAuthorityCheck () into the success function or have the success function call a new function that has the rest of the code.
$.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler. For more information on JSONP, see the original post detailing its use. Sending Data to the Server. By default, Ajax requests are sent using the GET HTTP method.
From your post I guess that you call a function that contains the $.ajax()
and try to return false
to that function. but you can't do that such way, because AJAX is asynchronous.
It's better to call a function from the ajax success function like following:
$.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');
}
}
Maybe you could try something like this (values 1 and 0 should be changed to the ones that you use):
success: function(result){
if(result === '1'){
// do something
}
else
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With