jsp 使用 ajax 两个同时请求只能得到一个响应

发布于 2024-09-28 03:11:43 字数 1766 浏览 4 评论 0原文

我有一个 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 指出的明显错误后,它就可以工作了。也在这篇文章中修复了它。

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;
    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);
    }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-10-05 03:11:43

您确实在同一网址上发送了两个请求并更新了同一 div。第一个不应该转到 qc1 并更新 qc1 吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文