1

複数(現在2つ)のajaxリクエストを一度に送信しようとしているjspページがありますが、2番目の応答しか得られないようです。 この男は私の問題を正確に説明しましたが、彼の解決策は私にはうまくいきませんでした。

これが私のjsコードです:


function createRequestObject(){
    var req;
    if(window.XMLHttpRequest){
        //For Firefox, Safari, Opera
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        //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() { 
            if(http.readyState == 4 && http.status == 200){
                var response = http.responseText;
                if(response){
                    document.getElementById(div).innerHTML = response;
                }
            }
        };

        http.send(null);
    }
}

そして、これが私がそのコードを呼ぶ方法です:

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>

一度に1つずつ呼び出すと、期待どおりに機能しますが、「両方」のリンクは、両方のindex.jspコードを実行していることがわかっていても、2番目のリクエストでのみ更新されます。

編集:わかりました、BalusCが指摘した明らかな間違いを修正した後、それは機能します。この投稿でも修正されました。

4

1 に答える 1

2

実際、同じURLで2つのリクエストを送信し、同じdivを更新しています。最初のものに行ってqc1更新するべきではありませんqc1か?

于 2010-10-14T12:18:02.773 に答える