views:

496

answers:

1

New to ajax, so asking a very basic question.

-- Is there no way to make a Synchronous ajax call (async:false) with timeout set on it.?

http://www.ajaxtoolbox.com/request/

Timeout works perfect with Asynchronous call though in my application, but for one particular scenario, I need a Synchronous call (the javascript should actually wait untill it hears back from the server), and this also works fine. But I need to handle a scenario where the sever could take long and a ajax timeout may be called.

Is there any other piece of standard documentation for ajax I could refer to?

Thanks

A: 

I don't believe it's possible to set a timeout on a synchronous call. When you set "async:false", I believe the browser actually locks up while waiting for the response. You should only use a synchronous request if you absolutely need to (because of the browser locking up).

Vivin Paliath
Thanks!! thats what I am finding everywhere, Also - is there any standard api / documentation out there we can look into for ajax...?
Zoom Pat
You can look up the documentation for XmlHttpRequest, but it's probably better to use a library that abstracts out a bunch of the details. jQuery, for example, has a very good AJAX framework. That's the one I'm more familiar with.jQUery's AJAX documentation:http://api.jquery.com/category/ajax/
Vivin Paliath
true - I am using jquery too for this...and the doc does not say this explicitly for timeout (http://api.jquery.com/jQuery.ajax/) and if you are familiar with validation framework, I am trying its 'remote:' feature to call the server for validation... the problem is that I validate the form with an onclick event of a button (not via submithandler) and after checking for form errors, I need to redirect to next page - in this case, it would redirect even before the remote returns back late (if the server is delayed)....so basically I want to wait for that remote call to get completed
Zoom Pat
Ah, I haven't tried the validation framework. Off the top of my head, I'd say that you should put the redirection code into your "success" handler (or equivalent). This way, you'll only redirect once the asynchronous call completes.
Vivin Paliath
hmm... well to make things more complicated I am also validating on the onkeyup event on that field... so in that case since it is the same remote validation being called, the redirecting on success will not be a good idea. But you did answer my initial question though... that timeout is not for synchronous calls... that helped me enough . I might need to explore the validation framework I guess.. Thanks!
Zoom Pat