【问题标题】:jsp with ajax two simultaneous requests only getting one response带有ajax的jsp两个同时请求只得到一个响应
【发布时间】:2010-10-14 12:14:58
【问题描述】:

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

这是我的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 指出的明显错误之后,它起作用了。在这篇文章中也修复了它。

【问题讨论】:

    标签: javascript ajax jsp xmlhttprequest simultaneous


    【解决方案1】:

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

    【讨论】:

    • 哇...我想为我浪费的两个小时哭泣。谢谢! (这个错误太明显了,我觉得有必要为自己辩护:之前有两个请求去正确的地方,但是js错了,然后当我将js修复到它应该工作的地方时,我已经重做了带有两个调用请求的页面并严重失败)
    • @epascarello:真的吗? “更新”是指作为第二个参数传递的 div id。我回滚了你的编辑。
    猜你喜欢
    • 2010-11-05
    • 2013-01-21
    • 2015-03-18
    • 1970-01-01
    • 2011-11-13
    • 2010-10-23
    • 2020-03-08
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多