相反:
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
alert("Call took " + new Date().getTime() - t1 + " milliseconds");
document.getElementById("info").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true);
xmlHttpRequest.send();
试试这个:
xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true);
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
alert("Call took " + new Date().getTime() - t1 + " milliseconds");
document.getElementById("info").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.send(null);
修复新日期
在你的代码中缺少括号(记住分开Mathematics
,Strings
)。尝试:
xmlHttpRequest.open("GET", "showStopsInfoPopup.do?stop="+stop, true);
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
alert("Call took " + (new Date().getTime() - t1) + " milliseconds");
document.getElementById("info").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.send(null);
测试 Ajax请求:
<div id="info"></div>
<script>var xhr, t1;
if(window.ActiveXObject){
try { xhr=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
try { xhr=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
}
} else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
xhr.open("GET", "teste.php", true);
t1 = new Date().getTime();//I start the timer that point to prevent the previous functions affect the end result
xhr.onreadystatechange=function(){
if(xhr.readyState == 4 && xhr.status == 200){
document.getElementById("info").innerHTML = "Call took " + (new Date().getTime() - t1) + " milliseconds";
}
}
xhr.send(null);
</script>
PHP(泰斯特。PHP):
<?php
sleep(5);
echo 'ok';
?>
结果:
IE8:5004毫秒
Chorme:5005毫秒
火狐:5014毫秒
IE7:5023毫秒
IE6:5053毫秒
试验后,得出结论认为,或许还有对服务器端的侧面,更确切地说在你的PHP。
为什么你使用jQuery标签,如果你不使用任何jQuery代码?如果您使用jQuery,只需使用['.load'](http://api.jquery.com/load/)方法。 – Blazemonger
我道歉。我对ajax比较陌生。所以我认为有人会知道jquery也会知道这个问题。当我搜索这个问题时,似乎很多有类似问题的人都在使用jQuery。 – Susie
延迟可能实际上是由于innerHTML,而不是ajax请求。我建议你分开计时。 – Christophe