C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Handling concurrent AJAX requests


Posted Date: 08 Dec 2008    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: gowthami chowdaryMember Level: Gold    
Rating: 1 out of 5Points: 6



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 AJAX Interaction. As arguments you pass in the URL to call and the function to call when the processing is done.
unction AJAX Interaction(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 "process me" 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 "process me" returns a document which is passed to the callback function that was specified when the AJAX Interaction 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 AJAX Interaction 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.




Responses

Author: ChandraShekar Thota    14 Jan 2009Member Level: Diamond   Points : 0
Good one

Chandrashekar Thota(Editor, MVP)




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Ajax  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to add service reference dynamically from user control? Microsoft Ajax
Previous Resource: Changing SiteMap File name
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use