简体   繁体   English

为什么 jQuery 在嵌套的 Ajax 函数中不起作用?

[英]Why jQuery does not work in nested Ajax functions?

While calling nested Ajax, which innermost ajax doesn't work.调用嵌套 Ajax 时,最里面的 ajax 不起作用。 As seen in the following example, I 'm calling getInnerResp into getResult .如以下示例所示,我将getInnerResp调用到getResult

Normally, when I debug with firebug is works interestingly.通常,当我使用 firebug 进行调试时,工作很有趣。 I think it behaves as async false but I setted async property as false didn't work again.我认为它表现为 async false 但我将 async 属性设置为 false 不再起作用。 Furthermore, I tried to get result set using callback function in getInnerResp function.此外,我尝试使用getInnerResp函数中的回调函数获取结果集。 Unfortunately I didn't succeed in any way.不幸的是,我没有以任何方式成功。 Also ftbl variable in getResult function returns null. getResult 函数中的 ftbl 变量也返回 null。 getInnerResp only returns followed result; getInnerResp只返回跟随的结果;

Result结果

<tr><td colspan='3'></td></tr><tr><td colspan='3'></td></tr><tr><td colspan='3'></td></tr><tr><td colspan='3'></td></tr>

Javascript Javascript

function getResult(year){

    var visible  = true;
    var table    = $(".tbl"), tbody = $(".result").find("tbody"), ftbl = "";
    
    table.find("tbody>tr").each(function(){
        var data = {
            course : $(this).find(".course").val(),
            year   : year,
            prog   : $(this).find(".program").val()
        }
        
        if(year.length < 1){
          alert("Year field can not be empty!!");
          visible = false;
          return false;
        }
        
        $.ajax({
            url : "result.php",
            method : "GET",
            data : data,
            contentType: "application/json; charset=utf-8",
            traditional : true,
            success : function(d){           
                tbody.append("<tr><td>" + course + "</td><td>" + d.enrolledStudent + "</td><td>" + d.failedStudent + "</td>");
                if(visible){
                    ftbl += getInnerResp(course, year);
                    console.log("inner" + ftbl);
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown){
                alert("Javascript runtime error: " + textStatus);
            }
        });
    });

    if(visible){
        tbody.append(ftbl);
    }
}

function getInnerResp(course, year){
  
    var tbl = "";
      
    var data = {
        course : course,
        year   : year
    }
    
    for(var i = 0; i < 5; i++){
        tbl += "<tr><td colspan='3'></td></tr>";
    }
      
    $.ajax({
        url : "course.php",
        method : "GET",
        data : data,
        contentType: "application/json; charset=utf-8",
        success : function(json){           
            $.each(json, function(i, val){
                tbl += "<tr><td>" + course + "</td><td colspan='2'>" + val + "</td></tr>";
            });
        },
        error : function(XMLHttpRequest, textStatus, errorThrown){
            alert(textStatus);
        }
    });
      
    return tbl;
}

This is your issue:这是你的问题:

ftbl += getInnerResp(course, year);

you try to assign a value generated in future (async) to a local variable.您尝试将未来(异步)生成的值分配给局部变量。

Try to move the task in the inner function, like:尝试在内部函数中移动任务,例如:

function getResult(year){

        var visible  = true;
        var table    = $(".tbl"), tbody = $(".result").find("tbody"), ftbl = "";

        table.find("tbody>tr").each(function(){
            var data = {
                course : $(this).find(".course").val(),
                year   : year,
                prog   : $(this).find(".program").val()
            }

            if(year.length < 1){
                alert("Year field can not be empty!!");
                visible = false;
                return false;
            }

            $.ajax({
                url : "result.php",
                method : "GET",
                data : data,
                contentType: "application/json; charset=utf-8",
                traditional : true,
                success : function(d){
                    tbody.append("<tr><td>" + course + "</td><td>" + d.enrolledStudent + "</td><td>" + d.failedStudent + "</td>");
                    if(visible){
                        //
                        // do the remaing in the inner task....
                        //
                        getInnerResp(course, year, tbody);
                    }
                },
                error : function(XMLHttpRequest, textStatus, errorThrown){
                    alert("Javascript runtime error: " + textStatus);
                }
            });
        });

    }

    function getInnerResp(course, year, tbody){

        var tbl = "";

        var data = {
            course : course,
            year   : year
        }

        for(var i = 0; i < 5; i++){
            tbl += "<tr><td colspan='3'></td></tr>";
        }

        $.ajax({
            url : "course.php",
            method : "GET",
            data : data,
            contentType: "application/json; charset=utf-8",
            success : function(json){
                $.each(json, function(i, val){
                    tbl += "<tr><td>" + course + "</td><td colspan='2'>" + val + "</td></tr>";
                });
                tbody.append(tbl);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown){
                alert(textStatus);
            }
        });

        console.log("inner" + tbl);;
    }

In getResult function ajax success callback, 'course' variable is being used but it was not defined anywhere.在 getResult 函数 ajax 成功回调中,正在使用“课程”变量,但未在任何地方定义。 It might be causing exception, which makes script execution to stop before calling getInnerResp.它可能导致异常,这使得脚本执行在调用 getInnerResp 之前停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM