How Do We Handle Concurrent AJAX Requests

  • 5 Jan, 2012

    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() {"GET", url, true); req.send(null);
    this.doPost = function(body) {"POST", url, true);
    req.setRequestHeader("Content-Type", " application/x-www-form-urlencoded");

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 All rights reserved.