// JavaScript Document
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlHttp=false;
}
}
else{
try{
xmlHttp=new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
if(!xmlHttp){
alert('cant create the object');
}
else{
return xmlHttp;
}
}
function process(){
if(xmlHttp.readyState==4 || xmlHttp.readyState==0){
food=encodeURIComponent(document.getElementById("userinput").value);
xmlHttp.open("GET","foodstore.php?food="+food,true);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(null);
}
else{
setTimeout(process(),1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement =xmlResponse.documentElement;
message=xml.DocumentElement.firstChild.data;
document.getElementById("underinput").innerHTML=message;
setTimeout(process(),1000);
}
else{
alert('something went wrong');
}
}
else{
alert('something went wrong 2');
}
}
在上面的代碼中,我xmlHttp.readyState==4
不執行,而執行在else
警報每次。AJAX xmlHttp.readyState == 4不執行
你是如何調用open(),送(),onreadystatechange的?表明 – epascarello