jsp 使用 ajax 两个同时请求只能得到一个响应
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实在同一网址上发送了两个请求并更新了同一 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 theqc1
one?