我有一个 jsp 页面,我试图一次发送多个(当前有两个)ajax 请求,但我似乎只收到第二个响应。 This这个人准确地描述了我的问题,但他的解决方案对我不起作用。
这是我的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 指出的明显错误后,它起作用了。也在这篇文章中修复了它。
最佳答案
您确实在相同的 URL 上发送了两个请求并更新了相同的 div。难道第一个不应该去 qc1
并更新 qc1
吗?
关于javascript - 带有ajax的jsp两个同时请求只得到一个响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932950/