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});
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.
精彩评论