1

我有一个 jsp 页面,我试图一次发送多个(当前两个)ajax 请求,但我似乎只得到第二个响应。 这个人准确地描述了我的问题,但他的解决方案对我不起作用。

这是我的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>

当我一次调用一个时,它们按预期工作,但“两者”链接仅随第二个请求更新,即使我知道它为两者运行 index.jsp 代码。

编辑:好的,在修复了 BalusC 指出的明显错误之后,它起作用了。在这篇文章中也修复了它。

4

1 回答 1

2

您确实在同一个 URL上发送了两个请求并更新了同一个 div。第一个不应该去那个qc1并更新那个qc1吗?

于 2010-10-14T12:18:02.773 回答