Hi I am trying to access one resource multiple times with with different parameters
In this case requesting
var domains = [
'host1',
'host2'
];
var requests = new Array();
for ( i in domains )
{
requests[i]=new request(domains[i]);
}
function request(site)
{
var url = 'get_remote_status.php?host='+site;
var queues = {};
http_request = new XMLHttpRequest();
http_request.open("GET", url, true, 'username', 'password');
http_request.onreadystatechange = function () {
var done = 4, ok = 200;
if (http_request.readyState == done && http_request.status == ok) {
queues = JSON.parse(http_request.responseText);
var queuesDiv = document.getElementById('queues');
print_queues(queues, queuesDiv, site);
}
};
http_request.send(null);
}
However, only one of of the requests is being handled by the code lambda. Chromium reports that both requests have been received and is viewable in the resourced pane.
Also if I make the request synchronous then it works fine. However this is not acceptable to the release code as a request may timeout.
Thanks
Define
http_request
usingvar
. Currently, you're assigning the XHR object to a global variable. Because of this, your script can only handle one XHR at a time.Relevant erroneous code:
Proposed change:
When you omit
var
before a variable, the variable will be defined in the global (window
) scope. If you usevar
before a variable, the variable is defined within the local scope (in functionrequest
, in this case).In fact it is possible to run multiple async xhr call but you have to give them an unique id as parameter to be able to store and load them locally in your DOM.
For example, you'd like to loop on an array and make a ajax call for each object. It's a little bit tricky but this code works for me.
You have to set each xhr object in a global variable object and define a value
xhrarray['xhr_'+item.mkdcck].uniqueid
to get its unique id and load its result where you want.Hope that will help you in the future.