XMLHttpRequest Asynchronous Multiple Request Sample Code

By Robbe Morris

Here's a quick snippet demonstrating how to process multiple XMLHttpRequests asynchronously in JavaScript.

<script type="text/javascript">
  
     var requests = new Array();    
    
     function ProcessUrls()
     {
         requests = new Array();
         var urls = new Array('http://www.url.com/a.aspx','http://www.url.com/b.aspx','http://www.url.com/c.aspx');

         for(i=0;i<urls.length;i++)
         {
             requests.push(new ProcessUrl(urls[i]));  
         }

     }

     function ProcessUrl(url)
     {
                      
            var http = new XMLHttpRequest();

            http.open("POST", url, true);
            
            http.onreadystatechange = function()
            {
                if (http.readyState == 4 && http.status == 200)
                {
                   alert(http.responseText);    
                }
            };

            http.send(null);
                              
     }

</script>

XMLHttpRequest Asynchronous Multiple Request Sample Code  (2872 Views)