skip to Main Content

I have an ajax call where success function is called twice. I tried to analyze but not getting why it is getting called twice.

I am using a for loop to call ajax functionality and inside that I am calling second ajax inside success function based on a condition (f==i+1). If the condition matches then it will call second ajax functionality, but what is happening is that, after the loop is over, inside the success of first ajax call is calling alert("Check call"); twice. It should call only once if i=1 in the loop.
What is happening in first loop is the condition doesn’t go to success and when the loop is over then it goes to success of first ajax as many time as loop happen and f become 2 and i+1 become 2 so it will check condition two times.

Any help would be appreciated.

    $(document).on("click", "#Rsub1", function (e) {
        if ($("#Rvno").val() == "" || $("#Rvno").val() == null) {
            alert("Please enter Vendor no");
        } else {
            for (var f = 0; f <= i; f++) {
                (function (index) {
                    $.ajax({
                        type: "POST",
                        url: "../Home/RaisePReq",
                        data: formData,
                        dataType: 'json',
                        contentType: false,
                        processData: false,
                        cache: false,
                        success: function (r) {
                            if (f == i + 1) {
                                alert("Check call");
                                var fieldvals
                                $.ajax({
                                    type: "POST",
                                    url: "../Home/gridtbl",
                                    data: JSON.stringify(fieldvals),
                                    contentType: "application/json; charset=utf-8",
                                    datatype: "json",
                                    success: function (r) {
                                        $('#grdtbl').html(r);
                                        //jQuery.noConflict();
                                        $('#ipcelltbl').DataTable({
                                            dom: 'Bfrtip',
                                            buttons: [
                                                'copy', 'csv', 'excel', 'pdf', 'print'
                                            ]
                                        });
                                    },
                                    failure: function () {
                                        alert("Error");
                                    }
                                });
                            }
                        },
                        failure: function () {
                            alert("Error");
                        }
                    });
                })(f);
            }
        }
    });

3

Answers


  1. if i = 1
    then
    for (var f = 0; f <= i; f++) {
    for (var f = 0; 0 <= 1; f++)
    for (var f = 1; 1 <= 1; f++)
    stop because    2 <= 1 is false.
    if i = 1 then for loop running 2 times.
    
    Login or Signup to reply.
  2. For loop first initialize the variable then checks for condition and then execute some code and increment or decrement variable. In your case when i=1, then on first execution it checks if f<=i and f is zero, it will execute first time. After that it increments f to 1
    and checks if f<=1 which is true, it will execute one more time. To prevent this you can add if (f<i) which stops after single execution only.

    Login or Signup to reply.
  3. Hi @sam Welcome to SO,

    Take a flag, initialize as false in start before success script, if got success then put flag true in the bottom end of success script , and also put a statement in the start of success script to check if flag true then exit etc.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search