Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax success: {return false;}

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?

like image 545
Rens Tillmann Avatar asked May 26 '12 14:05

Rens Tillmann


People also ask

How to return a value from an Ajax request?

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

Why does Ajax return undefined when I invoke it?

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.

How to call userauthoritycheck () and wait for the return value?

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.

What is the use of $Ajax?

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


2 Answers

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');
  }
}
like image 198
thecodeparadox Avatar answered Sep 30 '22 03:09

thecodeparadox


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;
}
like image 35
Gusgus Avatar answered Sep 30 '22 02:09

Gusgus