如何等待AJAX响应
我想知道如何在AJAX请求中暂停系统以等待服务器的响应如何等待AJAX响应,ajax,Ajax,我想知道如何在AJAX请求中暂停系统以等待服务器的响应 var method = childs[cont].getAttribute('method'); var address = childs[cont].getAttribute('address'); /* * Making the AJAX connection * and returning the results.
var method = childs[cont].getAttribute('method');
var address = childs[cont].getAttribute('address');
/*
* Making the AJAX connection
* and returning the results.
*/
phone = new ConstructorXMLHttpRequest();
onreadystatechange = function(){
switch(phone.readyState){
case 0: if(phone.readyState == 0){
break;
}
case 1: if(phone.readyState == 1){
break;
}
case 2: if(phone.readyState == 2){
break;
}
case 3: if(phone.readyState == 3){
break;
}
case 4: if(phone.readyState == 4){
if(phone.status == 200){
var val = phone.responseText;
alert([val,1]);
dataInsert(val);
break;
}
else{
alert("Problemas status:"+phone.status+" state:"+phone.readyState);
break;
}
}
}
};
phone.onreadystatechange = onreadystatechange;
if (method == 'POST'){
phone.open(method, address, true);
phone.setRequestHeader("Content-type", "multipart/form-data");
phone.send(xml2string(prepCall(childs[cont])));
}else if(method == 'GET'){
phone.open(method, address, true);
phone.setRequestHeader("Content-type", "multipart/form-data");
}
你的代码看起来不错。记住AJAX是异步的,所以不要暂停,只需将回调挂接到AJAX请求,请求完成后就会执行回调
在您的情况下,如果请求成功完成,它将发出警报并执行dateInsert函数。谢谢,我解决了我有时异步有时不异步的问题。