读书人

ajax中的xmlHttp.readyState == 1这步

发布时间: 2012-05-05 17:21:10 作者: rapoo

ajax中的xmlHttp.readyState == 1这步在chrome浏览器中没有执行怎么回事?
ajax代码:

JScript code
<SCRIPT type="text/javascript"><!--    var xmlHttp = "";    function creatXMLHttpRequest() {        if   (window.XMLHttpRequest)   {   //   Mozilla,   Safari,...              xmlHttp =new XMLHttpRequest();                      }          else if(window.ActiveXObject)   {   //   IE               xmlHttp =new ActiveXObject("Msxml2.XMLHTTP");           }        }    function startRequest() {        var queryString;        var domain = document.getElementById('domain').value;        queryString = "domain=" + domain;        creatXMLHttpRequest();        xmlHttp.open("POST","?action=do","true");        xmlHttp.onreadystatechange = handleStateChange;        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");        xmlHttp.send(queryString);    }    function handleStateChange() {        if(xmlHttp.readyState == 1) {            document.getElementById('result').style.cssText = "";            document.getElementById("result").innerHTML="正在查询……";        }        if(xmlHttp.readyState == 4) {            if(xmlHttp.status == 200) {                document.getElementById('result').style.cssText = "";                var allcon =  xmlHttp.responseText;                document.getElementById('result').innerHTML = allcon;            }        }    } //--></SCRIPT>



document.getElementById("result").innerHTML="正在查询……"; //主要是这行在ie下面执行时显示,但是在chrome浏览器下一直不显示“正在查询……”,有时候等待一两秒都是如此

[解决办法]
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById('result').style.cssText = "";
var allcon = xmlHttp.responseText;
document.getElementById('result').innerHTML = allcon;
}
}else{
document.getElementById('result').style.cssText = "";
document.getElementById("result").innerHTML="正在查询……";

}
}



这样写呢
[解决办法]
xmlHttp.readyState变化很快,可能就没有看到就更新了
你可以

function startRequest() {
var queryString;
var domain = document.getElementById('domain').value;
queryString = "domain=" + domain;
creatXMLHttpRequest();
xmlHttp.open("POST","?action=do","true");
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(queryString);
document.getElementById('result').style.cssText = "";
document.getElementById("result").innerHTML="正在查询……";
}

读书人网 >Ajax

热点推荐