views:

334

answers:

1

I have a function that issues an AJAX call (via jQuery). In the complete section I have a function that says:

complete: function(XMLHttpRequest, textStatus)
{
    if(textStatus == "success")
    {
        return(true);
    }
    else
    {
        return(false);
    }
}

However, if I call this like so:

if(callajax())
{
    //  Do something
}
else
{
    // Something else
}

The first is never called.

If I put an alert(textStatus) in the complete function I get true, but not before that function returns undefined.

Would it be possible to pass a callback function to my callajax() method? Like:

callajax(function(){// success}, function(){// error}, function(){// complete});

+4  A: 

complete is a callback function. It will be invoked by the Ajax object - asynchronously! - when the operation is complete. There is no way for you to catch the callback's result, only the Ajax object could do that.

Your callajax() function - you're not showing that function but I assume it simply makes the Ajax call - can not return the call's result (= the response headers and body), as the call will not have been finished yet when you exit the callajax() function.

Update: It is also well possible to make synchronous AJAX calls. Thanks to @Andris for pointing this out. In jQuery, you need to set the async option to false: Docs here. However, even those use the standard callback functions as far as I can see, so your desired method of returning false or true may still not work.

Pekka
Damn... Well I guess that's it then. Can I provide an additional argument (say, to call back) if it fails / succeeds / is complete?
Josh K
What you could do is, just put whatever is inside "Do something" and "Do something else" inside functions and call those within the complete callback function.Not sure if this would help in your case, just a guess.
TheCandyMan666
See me update and let me know if I'm on the right track. I'm trying to make the `callajax()` function modular so that I can use it for various sites / settings without needing to rewrite the inside `success`, `error`, and `complete` functions.
Josh K
Synchronous AJAX is totally possible, it is called SJAX and it is achieved with the third parameter of `XMLHttpRequest.open` - if it's true, then it's an asynchrounous call, otherwise it's synchrounous.
Andris
@Josh yes, you can put whatever you want into the `complete` function. There are more specific callbacks than just `complete`, though: `error()` and `success()`. Maybe those are what you're really looking for? http://api.jquery.com/jQuery.ajax/
Pekka
@Andris great to know, will update my answer.
Pekka
@Andris: How would I do that with a `$.ajax` call in jQuery?
Josh K
@Pekka: That's what I'm using (jQuery) however see my other comment and question edit.
Josh K
@Josh you'd have to delve into SJAX and find out whether your desired behaviour can be achieved with it - I don't know anything about it so I can't say. I guess it would work differently because there is no internal `readystatechange` that triggers the callback functions .... Maybe @Andris can add some info.
Pekka
You can find more info about SJAX from here: http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX - very simple and doesn't require jQuery or any other external library. AJAX is the complicated one, since it requires a callback function (onreadystatechanged) but SJAX is plain straight forward
Andris