日期:2014-05-16  浏览次数:20624 次

ajax初学者求教各位大侠
以下这段代码,为什么loading一直不出现?但是readyState==4的时候 是有的?为什么呢?
<script type="text/javascript">
var xmlhttp;
function fun100(url){
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','for.php?id='+url,true);
xmlhttp.onreadystatechange = byphp;
xmlhttp.send(null);
}
function byphp(){
if(xmlhttp.readyState==1){
document.getElementById('divc').innerHTML = "loading...";
}
if(xmlhttp.readyState==4){
var msg = xmlhttp.responseText;
document.getElementById('divc').innerHTML = msg;
}
}
</script>
</head>

<body>
<a href="#" onClick="fun100('a')">a</a>
<div id="divc"></div>
-----------------------------------------------------
<?php
$id = $_GET['id'];
for($i=1;$i<3;$i++){
echo $id;
sleep(2);
}
?>

------解决方案--------------------
    var xmlhttp;
    function fun100(url) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open('GET', '2.html', true);
        document.getElementById('divc').innerHTML = "loading...";

        xmlhttp.onreadystatechange = byphp;
        xmlhttp.send(null);
    }
    function byphp() {
        if (xmlhttp.readyState == 4) {
            var msg = xmlhttp.responseText;
            document.getElementById('divc').innerHTML = msg;
        }
    }