简体   繁体   English

jQuery Ajax调用成功功能内

[英]Jquery ajax call inside success function

I'm trying to make a ajax call inside success function. 我正在尝试在成功函数中进行ajax调用。 Inside the second ajax call I'm appending some data to a hidden field. 在第二个ajax调用中,我将一些数据附加到隐藏字段中。 Just after the for loop, alert shows the hidden field contains data but when I put the alert out side the secod ajax call, it shows empty. 在for循环之后,警报显示隐藏字段包含数据,但是当我将警报放在secod ajax调用之外时,它显示为空。 why the hidden field gets cleared? 为什么隐藏字段被清除?

<input type="hidden" id="hdnMenuChild" name="hdnMenuChild" />

            <script>
                function loadMenue() {

                    var url = "/UserManagement/GetUserMenue";

                    $.ajax({
                        type: "GET",
                        url: url,
                        contentType: "application/json; charset=utf-8",
                        //data: { initialApplicantID: initialApplicantID },
                        dataType: "json",
                        success: function (data) {
                            var mUL = "";
                            for (var x = 0; x < data.length; x++) {

                                if (data[x].MenueLevel == "0" && data[x].URL!="#") {

                                    mUL = mUL + '<li>';
                                    mUL = mUL + '<a href=' + data[x].URL + '>';
                                    mUL = mUL + '<span class="menu-text">' + data[x].Name + '</span>';
                                    mUL = mUL + '</a></li>';
                                }

                                if (data[x].MenueLevel == "0" && data[x].URL == "#") {

                                    mUL = mUL + '<li>';
                                    mUL = mUL + '<a href=' + data[x].URL + ' class="dropdown-toggle">';
                                    mUL = mUL + '<span class="menu-text">' + data[x].Name + '</span>';
                                    mUL = mUL + '<b class="arrow icon-angle-down"></b>';
                                    mUL = mUL + '</a>';
                                    mUL = mUL + '<ul class="submenu">';

                                    $.ajax({
                                        type: "GET",
                                        url: "/UserManagement/GetUserMenue?parentID=" + data[x].PermissionID,
                                        contentType: "application/json; charset=utf-8",
                                        //data: { initialApplicantID: initialApplicantID },
                                        dataType: "json",
                                        success: function (datay) {

                                            for (var y = 0; y < datay.length; y++) {

                                                $("#hdnMenuChild").val($("#hdnMenuChild").val() + '<li>');
                                                $("#hdnMenuChild").val($("#hdnMenuChild").val() + '<a href=' + datay[y].URL + '>');
                                                $("#hdnMenuChild").val($("#hdnMenuChild").val() + '<i class="icon-double-angle-right"></i>');
                                                $("#hdnMenuChild").val($("#hdnMenuChild").val() + datay[y].Name);
                                                $("#hdnMenuChild").val($("#hdnMenuChild").val() + '</a></li>'); 
                                            }
                                           alert($("#hdnMenuChild").val());//here, alert shows data.
                                        }
                                    });
                                   alert($("#hdnMenuChild").val());//alert shows empty data here
                                    mUL = mUL + '</ul>';
                                    mUL = mUL + '</li>';
                                }

                            }

                            $("#userMenue").html(mUL);
                        }
                    });


                }

Have you validated that the hidden fields aren't populated in the DOM after the completion of the whole process. 在整个过程完成之后,您是否已验证DOM中没有填充隐藏字段? Remember that $.ajax is asynchronous by default and the round trip time it takes for that second ajax call to return and setup your element in the DOM is longer than it takes to process the next function, in this case your following alert. 请记住,$。ajax默认情况下是异步的,并且第二个ajax调用返回并在DOM中设置您的元素所花费的往返时间比处理下一个函数所花费的时间更长,在这种情况下,您将收到以下警报。 So nothing has made it to the DOM by the time that alert is called. 因此,在调用警报时,没有任何东西可以到达DOM。 You could do this a couple of ways: 1) create a callback for the success, or 2) force the $.ajax to be non asynchronous by using "async: false" setting. 您可以通过以下两种方法执行此操作:1)创建成功的回调,或2)使用“ async:false”设置强制$ .ajax非异步。 I would personally opt for the callback approach and handle all success code in that. 我个人会选择回调方法并处理所有成功代码。 You will probably never get data returned from that alert though unless you make the ajax call non async, no matter how fast your connection to the server is.. even if it's local on the same machine. 除非您使ajax调用不异步,否则即使您与服务器的连接速度如此之快,也可能永远不会从该警报返回任何数据,即使它在同一台计算机上是本地的。

Also, I'm sure you have a good reason, but it seems very inefficient to loop over data returned from an ajax call, then call another ajax call for each item in that data. 另外,我确定您有充分的理由,但是遍历从ajax调用返回的数据,然后为该数据中的每个项目调用另一个ajax调用似乎效率很低。 I don't know if the server code isn't available or under your control, but a new end point should probably be created on the server to hit that will return all the data you're after in that second ajax call. 我不知道服务器代码是否不可用或不受您控制,但是可能应该在服务器上创建一个新的端点来命中,它将返回您在第二个ajax调用中所需要的所有数据。 Just a thought. 只是一个想法。

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

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