JavaScript

‘Synchronous AJAX’ or SJAX for Short

We had a problem at work the other day, we had a piece of JavaScript code that was making an AJAX style web server call, unfortunately other UI elements on the page required the results of that call to be available before the user attempts to use them. I tried searching the web for all sorts of things, ‘synchronous ajax,’ ‘ajax wait for response’ etc., leading to many explanations of the differences between asynchronous and synchronous requests, but no indication on how to do a synchronous request.

Finally, by looking through the JavaScript documentation for the XMLHttpRequest object and discovered that by simply adding an additional ‘false’ parameter to the normal http.open() call it forces the XMLHttpRequest object to run in synchronous mode. Exactly what we were looking for!

Here is an example: xmlhttp.open(“GET”, url, false);

This solved our problem completely, but since it was a difficult solution to find I figured I would post an example here in hopes of spreading the word of this useful tool.

Hope that helps!

Posted by Wally in Programming, 1 comment