Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何等待AJAX响应_Ajax - Fatal编程技术网

如何等待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.

我想知道如何在AJAX请求中暂停系统以等待服务器的响应

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函数。

谢谢,我解决了我有时异步有时不异步的问题。