5

I want this function to return wether or not the ajax call was succesful or not. Is there any way I can do this? My code below doesn't do this.

function myFunction(data) {
var result = false;
$.ajax({
    type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "url",
        data: data,
        error: function(data){
             result = false;
             return false;
        },
        success: function(data){
            result = true;
            return true;
        }
     });
     return result;
}
2

5 Answers 5

7

Unfortunately, you cannot return values to functions that wrap asynchronous callbacks. Instead, your success callback from the AJAX request will handoff the data and control to another function. I've demonstrated this concept below:

Definition for myFunction:

// I added a second parameter called "callback", which takes a function
 // as a first class object
function myFunction(data, callback) {
    var result = false;
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        url: "url",
        data: data,
        error: function(data){
            result = false;

            // invoke the callback function here
            if(callback != null)  {
                callback(result);
            }

            // this would return to the error handler, which does nothing
            //return false;
        },
        success: function(data){
            result = true;

            // invoke your callback function here
            if(callback != null) {
                callback(result);                
            }

            // this would actually return to the success handler, which does
              // nothing as it doesn't assign the value to anything
            // return true;
        }
     });

     // return result; // result would be false here still
}

callback function definition:

// this is the definition for the function that takes the data from your
 // AJAX success handler
function processData(result) {

    // do stuff with the result here

}

invoke your myFunction:

var data = { key: "value" }; /* some object you're passing in */

// pass in both the data as well as the processData function object
 // in JavaScript, functions can be passed into parameters as arguments!
myFunction(data, processData);
Sign up to request clarification or add additional context in comments.

Comments

4

No need call back. You can achieve this using async property.

function myFunction(){
    var retVal;
    $.ajax({
        url:url,
        method: GET/POST,
        data: data,
        async: false,
        success:function(response) {
            retVal = response;
        }
    });
    return retVal;
}

1 Comment

I don't think this isn't a good idea. Synchronous HTTP requests on a website are known to cause delays and hangs, because the rest of the code cannot execute until the server returns a response. This makes things slow and unusable. In fact, in jQuery 1.8 this behavior is deprecated.
1

You can specify async: false in the AJAX configuration, though the documentation notes that this will also lock the browser for the duration of the AJAX call, so it is not recommended.

3 Comments

hm ok. And there is no other possibility to achieve something like that then?
+1, Didn't know it was even possible to disable the async behaviour, even though I can't see I would ever opt to do it.
You can use Deferreds to return an object that you can attach success/failure callbacks later on: api.jquery.com/category/deferred-object
0

No, myFunction can't return the success of the ajax call, since the ajax call is done asynchronously.

You code will be executed in this order:

  1. var result = false;
  2. $.ajax sends the request to the server.
  3. return result (which is still set to false).
  4. When the response is received from the server, the success or error handler which contains result = false or result = true is called.

The correct way to handle this is to move any code that depends on the outcome of the ajax code into the success and error functions.

Comments

0

add this property that's help u

async: false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.