Take the 2-minute tour ×

I have a jsp page where I'm trying to send multiple (two currently) ajax requests at once, but I only seem to get the second response. This guy described my problem exactly, but his solution didn't work for me.

Here is my js code:

function createRequestObject(){
    var req;
        //For Firefox, Safari, Opera
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        req = new ActiveXObject("Microsoft.XMLHTTP");
        //Error for an old browser
        alert('Browser error');

    return req;

function sendRequest(method, url, d){
    var http = createRequestObject();
    var div = d;

    if(method == 'get' || method == 'GET'){
        http.open(method, url, true);

        http.onreadystatechange = function() { 
                var response = http.responseText;
                    document.getElementById(div).innerHTML = response;


And here is how I call that code:

QC1 Status: <div id='qc1'>blah</div> 
<br />UAT2 Status: <div id='uat2'>blah2</div>

<a onclick="sendRequest('GET','index.jsp?qc1.properties=true','qc1'); " href="#">qc1</a>
<a onclick="sendRequest('GET','index.jsp?uat2.properties=true','uat2'); " href="#">uat2</a>
<a onclick="sendRequest('GET','index.jsp?qc1.properties=true','qc1'); sendRequest('GET','index.jsp?uat2.properties=true','uat2'); " href="#">both</a>

When I call one at a time they work as expected, but the "both" link only updates with the second request, even though I know it runs the index.jsp code for both.

EDIT: Ok, after fixing the obvious mistake that BalusC pointed out, it works. Fixed it in this post too.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You're indeed sending two requests on the same URL and updating the same div. Shouldn't the first one go to the qc1 one and update the qc1 one?

share|improve this answer
wow... I want to cry about the two hours i've wasted. thank you! (this mistake was so obvious I feel the need to defend myself: had the two requests going to the right places before, but had the js wrong, and then by the time I had fixed the js to where it should work, I had redone the page with the two calling requests and failed horribly) –  MYou Oct 14 '10 at 12:20
second qc1 should be uat2., editing your answer –  epascarello Oct 14 '10 at 12:25
@epascarello: really? With "update" I meant the div id which is passed as second argument. I rollbacked your edit. –  BalusC Oct 14 '10 at 12:32
Whoops, misread that! –  epascarello Oct 14 '10 at 16:57
add comment

Your Answer


