开发者

How wait to AJAX response

i wanna know how i can make a pause in the system to wait the answer from the server in an AJAX request?

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;
                        }
                    }
 开发者_如何学Python               }
            };
            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");
            }


Your code looks OK. Remember AJAX is asynchronous, so instead of actually pausing, just hook a callback to your ajax request which will be executed once the request is completed.

In your case, if the request is completed successfully it will alert and execute the dateInsert function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜