Sending Asynchronous Requests

written by: Carol Rudenberg; article published: year 2006, month 10;



In: Categories » Computers and technology » AJAX and JavaScript » Sending Asynchronous Requests

Synchronous requests are easier to use than asynchronous requests because they return data directly and remove the hassle of creating callback functions. However, they aren't the standard use case for XMLHttpRequest because the entire browser is locked while the request is happening. There are some circumstances in which blocking is useful (mainly when a decision needs to be made before the current function ends), but in most cases, you'll want these requests to happen in the background. An asynchronous request allows the browser to continue running JavaScript code and users to continue interacting with the page while the new data is loaded. With the proper user interface, asynchronous communication allows an AJAX application to be useful even when the user's connection to the site is slow.

To make an asynchronous call, we need to accomplish two tasks: set the asynchronous flag on open to TRue, and add a readyStateChanged event handler. This event handler will wait for a ready state of 4, which means the response is loaded. It will then check the status property. If the status is 200, we can use responseText; if it's another value, we have an error, so we'll need to create an alert dialog to show it. An asynchronous call to test.php is shown in the following listing. The initXMLHttpClient function described here is used to create our XMLHttpRequest object.

Making an Asynchronous Request

1 var req = initXMLHttpClient();
2 req.onreadystatechange = function() {
3 if (req.readyState == 4) {
4 if (req.status == 200) {
5 alert(req.responseText);
6 } else {
7 alert('Loading Error: ['+req.status+'] '
8 +req.statusText);
9 }
10 }
11 }
12 req.open('GET','test.php',true);
13 req.send(null);

Although this code gets the job done, it's not a great long-term solution because we will have to write a new onreadystatechange method for each call. The solution to this is to create our own HttpClient class that wraps XMLHttpRequest. Such a class gives us an easy-to-use API and a property to use for the callback that has to deal only with successful requests. Just adding some helper methods would be a simpler solution, but that's not a possibility because IE doesn't allow you to add methods to an ActiveX object.

A sample XMLHttpRequest wrapper class is shown in the following listing. The main features of the HttpClient class are a callback property that is called when a successful asynchronous request is complete and a makeRequest method that combines the open and send functions. It also provides event properties that are called when a request is made (onSend), when it ends (onload), and when an errors occurs (onError). A default onSend and onLoad implementation is provided, which creates a basic loading message while requests are being made.

HttpClient XMLHttpRequest Wrapper

1 function HttpClient() { }
2 HttpClient.prototype = {
3 // type GET,POST passed to open
4 requestType:'GET',
5 // when set to true, async calls are made
6 isAsync:false,
7
8 // where an XMLHttpRequest instance is stored
9 xmlhttp:false,
10
11 // what is called when a successful async call is made
12 callback:false,
13
14 // what is called when send is called on XMLHttpRequest
15 // set your own function to onSend to have a custom loading
16 // effect
onSend:function() {
17 document.getElementById('HttpClientStatus').style.display =
18 'block';
19 },
20
21 // what is called when readyState 4 is reached, this is
22 // called before your callback
23 onload:function() {
24 document.getElementById('HttpClientStatus').style.display =
25 'none';
26 },
27
28 // what is called when an http error happens
29 onError:function(error) {
30 alert(error);
31 },
32
33 // method to initialize an xmlhttpclient
34 init:function() {
35 try {
36 // Mozilla / Safari
37 this.xmlhttp = new XMLHttpRequest();
38 } catch (e) {
39 // IE
40 var XMLHTTP_IDS = new Array('MSXML2.
XMLHTTP.5.0',
41 'MSXML2.XMLHTTP.4.0',
42 'MSXML2.XMLHTTP.3.0',
43 'MSXML2.XMLHTTP',
44 'Microsoft.XMLHTTP');
45 var success = false;
46 for (var i=0;i < XMLHTTP_IDS.length &&
!success; i++) {
47 try {
48 this.xmlhttp = new ActiveXObject
(XMLHTTP_IDS[i]);
49 success = true;
50 } catch (e) {}
51 }
52 if (!success) {
53 this.onError('Unable to create XMLHttpRequest.');
54 }
55 }
56 },
57
58 // method to make a page request
59 // @param string url The page to make the request to
60 // @param string payload What you're sending if this is a POST
61 // request
62 makeRequest: function(url,payload) {
63 if (!this.xmlhttp) {
64 this.init();
65 }
66 this.xmlhttp.open(this.requestType,url,this.isAsync);
67
68 // set onreadystatechange here since it will be reset after a
69 //completed call in Mozilla
70 var self = this;
71 this.xmlhttp.onreadystatechange = function() {
72 self._readyStateChangeCallback(); }
73
74 this.xmlhttp.send(payload);
75
76 if (!this.isAsync) {
77 return this.xmlhttp.responseText;
78 }
79 },
80
81 // internal method used to handle ready state changes
82 _readyStateChangeCallback:function() {
83 switch(this.xmlhttp.readyState) {
84 case 2:
85 this.onSend();
86 break;
87 case 4:
88 this.onload();
89 if (this.xmlhttp.status == 200) {
90 this.callback(this.xmlhttp.responseText);
91 } else {
92 this.onError('HTTP Error Making Request: '+
93 '['+this.xmlhttp.
status+']'+
94 '+this.xmlhttp.
statusText));
95 }
96 break;
97 }
98 }
99 }

The HttpClient class contains comments explaining its basic functionality, but you will want to look at a couple of areas in detail. The first areas are the properties you'll want to set while interacting with the class; these include the following:
  • requestType(line 4). Used to set the HTTP request type, GET is used to request content that doesn't perform an action whereas POST is used for requests that do.
  • isAsync(line 6). A Boolean value used to set the request method. The default is false, which makes an synchronous request. If you're making an asynchronous request, isAsync is set to true. When making an asynchronous request, you also need to set the callback property.
  • callback(line 12). This property takes a function that takes a single parameter result and is called when a request is successfully completed.

Lines 1631 contain simple functions for handling some basic user feedback. When a request is sent to the server, a DOM element with the ID of HttpClientStatus is shown (lines 1619). When it completes, it is hidden again (lines 2326). The class also defines a function to call when an error happens (lines 2931); it creates an alert box with the error message. Common errors include receiving a 404 page not found HTTP error message or not being able to create an XMLHttpRequest object. The implementation of these three functions is simple, and you'll likely want to override them with more sophisticated application-specific versions.

Lines 3356 contain the init method, which is identical to the initXMLHttpClient function we created here, except for what it does with its error message. Now it sends it to the onError method. You won't be dealing with this function directly because the makeRequest method will take care of it for you. The makeRequest method (lines 6279) is your main interaction with the class. It takes two parameters: a URL to which to make the request and a payload that is sent to the server if you're making a POST request. The actual implementation is a more generic version of the code shown above. The _readyStateChangeCallback (lines 8299) method is set as the readyState handler by makeRequest. It handles calling onSend when the initial request is sent and then calling onload when the request completes. It also checks for a 200 HTTP status code and calls onError if some other status is returned.

The following listing uses the HttpClient class and shows its basic usage. A wrapper class like this helps cut down the amount of code you need to write per request while giving a single place to make future changes.

1
2
3 Simple XMLHttpRequest Wrapper Test Page
4
5
6
7
19
20


21 Make an Async Test call
22

23
24

Using the HttpClient XMLHttpRequest wrapper is a simple task. You start by including it in the header of your HTML page (line 5), and then you can proceed to use it. You do this by creating an instance of the class (line 9), configuring its basic properties (in this case, setting isAsync to true (line 10)), and then setting up some code to call makeRequest. In most cases, this code will be contained in a function so that it can be tied to a user-driven event, such as clicking a link. The call is made by the test function (lines 1217); the test function first sets up a callback to run when the request is complete (lines 1315), and then it calls makeRequest (line 16), which starts the AJAX call.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. How I Decide Which Open Source AJAX Library to Use
The company I work for, Uversa Inc., is based around General Public License (GPL) software, so when I pick any library, it first needs to be compatible with the GPL. Because the GPL is so widespread, many licenses are compatible with it. (See www.fsf.org/licensing/licenses/index_html#GPLCompatibleLicenses for more information.) However, because licensing is a hard rule, you should always start your search by limiting it to the ones th...

2. Goals of AJAX
First and foremost, AJAX is about improving user experience; user experience improvements fall into two categories: making current tasks easier and making previously impossible tasks possible. Obviously, it is easier to focus on making current tasks easier. In Web development environments, this can be further broken down into two main goals: increasing interactivity and decreasing the time required to perform a task. In nonintranet cases, you may also have a related technical goal of reducing bandwidth use; by transferring less...

3. XMLHttpRequest Overview
Originally, Microsoft designed XMLHttpRequest to allow Internet Explorer (IE) to load XML documents from JavaScript. Even though it has XML in its name, XMLHttpRequest really is a generic HTTP client for JavaScript. With it, JavaScript can make GET and POST HTTP requests. (For POST requests, data can be sent to the server in a format of your choosing.) The main limitations to XMLHttpRequest are due to the browser security sandbox. It can make only HTTP(S) requests (file URLs, for example, won't work), and it can make requests o...

4. Promises and Problems of Combining AJAX with Other New Technologies
As you work with AJAX, you may hear of related technologies that you can use with AJAX. They fit into two main groups: mature technologies that are widely available in many browsers today, and new technologies that are available only on a specific browser. The mature technologies include Java and Flash. (Flash is the most important because its plug-in is widely installed, and its design is optimized for providing interactive elements and animations to Web sites.) Java can also be used to add interactivity to sites, but its popula...

5. JavaScript as a Primary Development Language for AJAX applications
JavaScript is a powerful scripting language, but deserved or undeserved, it has gained a bad reputation. If you take some time to look at JavaScript with a fresh eye, you will notice that most of its problems no longer exist. The core language is now standardized with the European Computer Manufacturer's Association (ECMA) standards group and is supported on all modern browsers. Of course, these browsers also support older proprietary syntaxes, and you should avoid these as much as possible. Keeping to the standardized interfac...

6. What Is Ajax
Ajax stands for Asynchronous Javascript And XML. Although strictly speaking Ajax itself is not a technology, it mixes well-known programming techniques in an uncommon way to enable web developers to build Internet applications with much more appealing user interfaces than those to which we have become accustomed. When using popular desktop applications, we expect the results of our work to be made available immediately, without fuss, and without us having to wait for the whole screen to be redrawn by the program. While us...

7. Integrating AJAX into a Framework
Whether you're planning to add only a few simple AJAX features or use AJAX throughout your site, integrating it into your current Web site design is a must. The more formal the framework, the harder the process isespecially if your framework provides a front controller that is heavily optimized for generating HTML. Frameworks without a front controller have an easier time incorporating AJAX because they can add a new entry point just for AJAX; many AJAX Remote Procedure Call (RPC) implementations provide code to help do this....