How do I handle concurrent AJAX requests?

Answer

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();
}

All ajax Questions

Ask your interview questions on ajax

Write Your comment or Questions if you want the answers on ajax from ajax Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---