嵌套的$ .get请求无法返回响应值

时间:2018-07-06 09:49:17

标签: javascript jquery asynchronous get

关于How do I return the response from an asynchronous call?这篇文章,响应可以在传递给回调之前进行处理。

如果我从不在程序中使用循环,则有可能获得该值,但是这里的程序必须通过3个API循环才能获取所有信息。我必须在循环外进行打印才能将所有信息返回到表中,否则我只会在浏览器中得到一行信息

这是代码

$(function () {
        $.get(urlVS + "/retrieve_current_user_email", function (result) {
            var jsonObj = JSON.parse(result);
            userEmail = jsonObj.email;
            $.get(urlVS + "/retrieveFormList?email=" + userEmail, function (retrieveFormListResult) {

                getResult = JSON.parse(retrieveFormListResult);
                var k = 0;
                var l = 0;
                var unattemptedTableBody = "<p><table class='table table-striped table-bordered' style='width:100%; margin-top:26px;'>" +
                    "<thead><center style='margin-top: 16px;'><u><b>Un-attempted Votes</b></u></center>" +
                    "<tr><th>No</th><th>Title</th><th>Creator</th><th>Time Created</th><th>Action</th></tr></thead><tbody>";

                var attemptedTableBody = "<p><table id='attempt' class='table table-striped table-bordered' style='width:100%; margin-top:26px;'>" +
                    "<thead><center style='margin-top: 16px;'><u> <b>Attempted Votes</b></u></center>" +
                    "<tr><th>No</th><th>Title</th><th>Creator</th><th>Time Created</th><th>Action</th></tr></thead><tbody>";


                for (var i = 0; i < getResult.message.length; i++) {
                    //check if user has attempted the question
                    formId = getResult.message[i].formid;
                    date = getResult.message[i].created_time;
                    title = getResult.message[i].title;
                    creator = getResult.message[i].creator;

                    $.get(urlVS + "/retrieveResult2?formid=" + formId + "&email=" + userEmail, function (retrieveResult) {
                        var objRetrieveResult = JSON.parse(retrieveResult);
                        getLastIndexDot = date.lastIndexOf(".");
                        date = date.substring(0, getLastIndexDot);
                        for (var j = 0; j < objRetrieveResult.surveyInfo.userUnFillList.length; j++) {

                            if (objRetrieveResult.surveyInfo.userUnFillList[j] != null) {
                                if (objRetrieveResult.surveyInfo.userUnFillList[j].email == userEmail) {
                                    console.log(objRetrieveResult.surveyInfo.userUnFillList[j].email)
                                    k += 1;
                                    unattemptedTableBody += "<tr><td>" + k + "</td><td>" + title + "</td><td>" + creator + "</td><td>" + date + "</td>" +
                                        "<td><a href='/votesystem/Go_to_Answers'><button type=\"button\" class=\"btn btn-primary btn-block\">Start Voting</button></a></td></tr>";
                                    processGetRequestValue(unattemptedTableBody);
                                }
                            }
                            if (objRetrieveResult.surveyInfo.userFilledList[j] != null) {
                                if (objRetrieveResult.surveyInfo.userFilledList[j].email == userEmail) {
                                    l += 1;
                                    attemptedTableBody += "<tr><td>" + l + "</td><td>" + title + "</td><td>" + creator + "</td><td>" + date + "</td>" +
                                        "<td><a href='/votesystem/Go_to_Answers'><button type=\"button\" class=\"btn btn-primary btn-block\">View Result</button></a></td></tr>";
                                }
                            }
                        }
                    });
                }


                unattemptedTableBody += "</tbody></table></p>";
                attemptedTableBody += "</tbody></table></p>";
                $("#attempt").html(attemptedTableBody);
                $("#home").html(unattemptedTableBody);

我无法将响应的值返回到我的表,请忽略代码的右括号。顺便说一句,如果我以这种方式使用get请求,则该方法不合适还是无效?有更好的主意以更好的方式执行这些请求吗?

0 个答案:

没有答案