Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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
在javascript中验证多个ajax调用是否成功完成_Javascript_Ajax - Fatal编程技术网

在javascript中验证多个ajax调用是否成功完成

在javascript中验证多个ajax调用是否成功完成,javascript,ajax,Javascript,Ajax,在我的应用程序中,我将进行两次ajax调用,以基于另一个变量值从数据库中获取一些数据。 代码部分: var xmlHttp; var redirect; function populate_site(obj, passedselect) { xmlHttp = GetXmlHttpObject(); var site_type; if (obj.value) { site_type = obj.value }else { si

在我的应用程序中,我将进行两次ajax调用,以基于另一个变量值从数据库中获取一些数据。 代码部分:

var xmlHttp;
var redirect;

function populate_site(obj, passedselect) {
    xmlHttp = GetXmlHttpObject();

    var site_type;

    if (obj.value) {
        site_type = obj.value
    }else {
        site_type = document.app.site_type.value;
    }

    var url="/cgi-bin/Web/Begin.cgi";
    url=url+"?redirect=Get_Site_List";
    url=url+"&site_type=" + site_type;

    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);    
}

function stateChanged() {     
    if (xmlHttp.readyState == 4){ 
        document.getElementById('site_id').innerHTML = xmlHttp.responseText;
    }
}

function Populate_Process_List(obj, process_id_param) {    
    var action_id;

    if (obj.value) {
        action_id = obj.value
    }else {
        action_id = document.app.action_id.value;
    }

    xmlHttp = GetXmlHttpObject();

    if (obj.value == '') {
        document.getElementById('process_id').innerHTML = ' ';
        return false;
    }

    var url="/cgi-bin/Web/Begin.cgi";
    url=url+"?redirect=Fetch_User_Process_List";
    url=url+"&action_id=" + action_id;
    url=url+"&process_id=" + process_id_param;
    url=url+"&gp_flag=" + 'YES';                                         
    xmlHttp.onreadystatechange= Process_Data_StateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);        
}

function Process_Data_StateChanged() {
    if(xmlHttp.responseText != 'NA') {
        if (xmlHttp.readyState == 4){
            document.getElementById('process_id').innerHTML = xmlHttp.responseText;
        }
    }
}

function GetXmlHttpObject(){
    var xmlHttp1=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp1=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp1;
}
以及使用JavaScript验证site_id和process_id变量

所面临的问题是,当用户在ajax调用加载未完成之前按下submit按钮时,因为传递open方法的async参数的值为true。即使在使用JavaScript验证site_id和process_id变量时,也无法捕获

验证代码:

var siteaddindex = document.app.site_id[document.app.site_id.selectedIndex].value;

if(siteaddindex == "null"){
    alert("Please select site location.");
    document.app.site_id.focus();
    return false;
}

var process_id = document.app.process_id.value;

if (process_id == '') {
    alert("Please select process");
    return false;
}

请告诉我如何对上述字段进行验证,因为这些字段是必需的。

我只是略过了一下问题,但您是否考虑过使用承诺?只有在加载完成后才启用按钮因为在我的编程中有两个ajax调用,我如何才能知道两个ajax调用是否都已完全加载。请注意,现在有一个库async.js,它实现了我上面给出的答案,并为您提供了更多控制这类事情的方法。