0

Is there any way I can run this code with async:false? I notice the jQuery manual specifies that it must be true, when passing the call return to a variable, but that is painful considering the method is meant to be generic.

ajaxCall.classFunction = function( $class, $function, $params){

//Ensure no errors occured
$params = typeof($params) != 'undefined' ? $params : false;

//Ajax Call for data
var $r =$.ajax({
    type:   'POST',
    url:    'json.php?c='+$class+'&func='+$function,
    data:   $params,
    timeout:5000,
    async:false,
}).responseText;
return $r;
4
  • what is it that you're trying to do here? are you trying to POST the function to the PHP page, or do you want to call the function after POSTing? Commented Sep 28, 2010 at 15:16
  • You can set ajax calls to be synchronous by default via jQuery's $.ajaxSetup()-method. This is a bad idea in general, though. Commented Sep 28, 2010 at 15:17
  • @dave a js function which posts class/function/arguments to the server, which then executes said class/function with said arguements. Commented Sep 28, 2010 at 20:27
  • yes, take a look at the documentation, about halfway down is an example that uses a success handler: api.jquery.com/jQuery.ajax Commented Sep 28, 2010 at 20:35

1 Answer 1

1

No, you will have to specify success callback. With async set to true your outer method is not blocked and returns immediately without waiting for the actual response for your ajax call.

Sign up to request clarification or add additional context in comments.

3 Comments

did you mean: With async set to true your outer method is not blocked and returns immediately.
So what your saying is that it should be possible to add a success handler which can then assign the output to a variable( or etc )?
Yes. Inside callback you can reference a variable that is declared outside it (you might want to read about closures - en.wikipedia.org/wiki/Closure_(computer_science)). But your ajax call has to by synchronous.

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.