Related Posts by Categories
ajax video tutorials
- Creating an Ajax-driven Drop-down Menu System - 40
- Communicating With the Shopping Cart on the Server - 39
- Handling Drops - Ajax Tutorial 38
- Enabling Dragging Using Ajax | Ajax Tutorial 37
- Handling Mouse Events - Ajax Tutorial 36
- Creating a Shopping Cart - Buy item by dragging it to the shopping cart
- Downloading Images Using Ajax - Ajax Tutorial 34
- Returning JavaScript Objects From the Server AjaxTutorial 33
- Handling Multiple XMLHttpRequest Requests - Ajax Power - 32
- Using Two XMLHttpRequest objects - Ajax tutorial - 31
- Calling a Different Domain Safely - Getting Ajax Power - 30
- Connecting to Google Suggest - Live Google Search - Ajax Training 29
- Creating a Live Search Part.2 - 28 Using Live Google Search
- Creating a Live Search Part.1 - Live Search using Google
- Handling Head Requests for More Data - Ajax training 26
- Determining Which Browser the User Has - Ajax training 25
- Passing Data to the Server with POST - Ajax Training - 24
- Passing Data to the Server with GET - AJAX TUTORIAL - 23
- Extracting Data From XML - ajaxtraining.blogspot.com - 22
- Fetching XML Data From the Server - Ajax Training - 21
- Storing Ajax Data in XML - Ajaxtraining.blogspot.com - 20
- Using Server-Side Scripting - Ajaxtraining.blogspot.com -19
- Getting XMLHttpRequest Objects in Other Ways Ajaxtutorial 18
- Selecting Relative or Absolute URLs - Ajax tutorial - 17
- Decoding the Fetched Data - Ajax Tutorial - 16
2 comments:
How do I handle concurrent AJAX requests?
With JavaScript you can have more than one AJAX request processing at a single time. In order to insure the proper post processing of code it is recommended that you use JavaScript Closures. The example below shows an XMLHttpRequest object abstracted by a JavaScript object called AJAXInteraction. As arguments you pass in the URL to call and the function to call when the processing is done.
function AJAXInteraction(url, callback) {
var req = init();
req.onreadystatechange = processRequest;
function init() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function processRequest () {
if (req.readyState == 4) {
if (req.status == 200) {
if (callback) callback(req.responseXML);
}
}
}
this.doGet = function() {
req.open("GET", url, true);
req.send(null);
}
this.doPost = function(body) {
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "
application/x-www-form-urlencoded");
req.send(body);
}
}
function makeRequest() {
var ai = new AJAXInteraction("processme",
function() { alert("Doing Post Process");});
ai.doGet();
}
The function makeRequest() in the example above creates an AJAXInteraction with a URL to of "processme" and an inline function that will show an alert dialog with the message "Doing Post Process". When ai.doGet() is called the AJAX interaction is initiated and when server-side component mapped to the URL "processme" returns a document which is passed to the callback function that was specified when the AJAXInteraction was created.
Using this closures insures that the proper callback function associated with a specific AJAX interaction is called. Caution should still be taken when creating multiple closure objects in that make XmlHttpRequests as to there is a limited number of sockets that are used to make requests at any given time. Because there are limited number of requests that can be made concurrently. Internet Explorer for example only allows for two concurrent AJAX requests at any given time. Other browsers may allow more but it is generally between three and five requests. You may choose to use pool of AJAXInteraction objects.
One thing to note when making multiple AJAX calls from the client is that the calls are not guaranteed to return in any given order. Having closures within the callback of a closure object can be used to ensure dependencies are processed correctly.
There is a discussion titled Ajaxian Fire and Forget Pattern that is helpful.
Post a Comment