Requesting ASP.NET AJAX synchronously

Although the acronym AJAX refers directly to an asynchronous request (Asynchronous JavaScript and XML), it may be needed in some cases to use all the technology sitting behind the term with one small detail because we want to make a synchronous request.
If ever the term AJAX did not stick properly to the desired function, we can always appoint our technique SJAX (Synchronous JavaScript and XML). We assume that our readers have a good level of knowledge with the .NET platform and the ASP.NET and AJAX technologies. In addition, note that this tip is based on our example on the Framework.NET 3.5 with the Microsoft Visual Studio 2008 IDE. Consider meaning of terms "asynchronous AJAX" and "synchronous AJAX " knowing that when an requesting AJAX, the web user does not notice a refresh of the page, but may notice that he hasinnate communication with the server. This dynamism has especially the advantage that our users can continue to operate the webpage without having to wait for server responses.
Now suppose we have a special case in which we must necessarily obtain the server response before releasing the client process. This can be for example the case of a "checkbox" where one wants to control the value of the event of the click itself and this based upon server-side processing. To do this we must launch an AJAX request in the event of click and wait for the server response without giving the possibility to the user to make another action before leaving the event. In other words, the request and response to the server must be within the same function and not a deferred basis as is the case with the asynchronous request. Of course, you can always try to use solutions based on synchronous AJAX request ()not provided specifically for use with ASP.NET but the problem is that there is a risk of losing the advantage of simplicity provided by the platform : having to manage the use of standard web services in .NET.

In short, what we propose is rather a technique based on the intercalation of trigger for the web application different from the one the used by default to handle AJAX request in ASP.NET. It is called « Sys.Net.XMLHttpSyncExecutor » and differs in its use of ASP.NET, of what is usually done with an asynchronous AJAX request. We will use our example of asynchronous request "HelloWorld"

ApplicationWebAjax.ServiceWebAjax.HelloWorld(RequestDate, Requestcounter, HelloWorld_SucceededCallback, FailedCallback);

In this case we call the function "HelloWorld" with two parameters and returns a message as a string. The function is located under the web service ApplicationWebAjax.ServiceWebAjax ».
If you want to do so we call our web service synchronously, we must proceed as follows and this after referring to the JavaScript class Sys.Net.XMLHttpSyncExecutor in our web page:

var defaultExecutorType = Sys.Net.WebRequestManager.get_defaultExecutorType();
var request = ApplicationWebAjax.ServiceWebAjax._staticInstance.HelloWorld(RequestDate, Requestcounter);
if (request._executor.get_responseAvailable()) {
var resultat = request._executor.get_object().d;

Note that we insert our request trigger with the method: « set_defaultExecutorType » and in the end we get the default one, to ensure that any subsequent calls work correctly with the asynchronous mode.
In addition, we note that the reading of the function results looks like a « request._executor.get_object().d » and that it must go through « _staticInstance » to call the "HelloWorld".
Unfortunately, the technique presented here is not supposed to run on all versions of web browsers, but intervention on the « XMLHttpSyncExecutor » JavaScript class could improve this
The solution was tested with various versions of Internet Explorer and also with some recent versions of Mozilla Firefox. Even if tweaking AJAX ASP.NET allows us to make a synchronous call, it is strongly advised to practice these AJAX request in extreme cases and especially with simple queries. Note that during the time needed to make a request, the web page would be blocked and unusable because the browser remains active until the end of the interpretation of your request function. Thanks to n00r for this tip.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Requesting ASP.NET AJAX synchronously », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (