Jquery ajax call inside success function

chamara

I'm trying to make a ajax call inside success function. Inside the second ajax call I'm appending some data to a hidden field. 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. 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);
                        }
                    });


                }
ubergoober

Have you validated that the hidden fields aren't populated in the DOM after the completion of the whole process. 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. So nothing has made it to the DOM by the time that alert is called. 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. 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.

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. 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. Just a thought.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ajax call inside ajax success jquery then ajaxstart function not working

Jquery ajax call inside a then function

jquery, ajax call inside a function: can't access a global variable passed as argument to outer function from ajax success

jQuery ajax post inside each function, success continue in each

Calling function inside jQuery ajax success response works once but not twice

ajax success error function doesn't call up in jquery

JQuery .ajax() function call not working, but success method runs

AJAX Success call a defined function

AJAX call with $(this) parameter in SUCCESS function

Cannot call a function in AJAX success

Ajax call inside an ajax success not working

Jquery Pjax - Ajax success function

Cannot access view model's observables and functions inside Ajax's success call back function- Knockout

Change event inside ajax success function

Ajax Call Success Function Firing Twice

Ajax call function on success return false not working

Symfony / Ajax : call function on success response

ajax error results in success function call

successful ajax call not returning to success function

jQuery: Return data after ajax call success

jQuery Ajax call success then re-direct

Global Variable not affected in JQuery Ajax Success Function

jQuery ajax success callback function definition

reading a string table jquery ajax success function

Ajax success and error function is not calling properly in jquery

jQuery AJAX return the function as data upon success

how to write jQuery ajax's success function

Ajax using JQuery success function is not called

jQuery AJAX never runs success function