Execute function after Ajax call is complete

后端 未结 6 2011
遇见更好的自我
遇见更好的自我 2020-11-30 03:16

I am new to Ajax and I am attempting to use Ajax while using a for loop. After the Ajax call I am running a function that uses the variables created in the Ajax call. The fu

相关标签:
6条回答
  • 2020-11-30 03:26

    Add .done() to your function

    var id;
    var vname;
    function ajaxCall(){
    for(var q = 1; q<=10; q++){
     $.ajax({                                            
             url: 'api.php',                        
             data: 'id1='+q+'',                                                         
             dataType: 'json',
             async:false,                    
             success: function(data)          
             {   
                id = data[0];              
                vname = data[1];
             }
          }).done(function(){
               printWithAjax(); 
          });
    
    
    
     }//end of the for statement
    }//end of ajax call function
    
    0 讨论(0)
  • 2020-11-30 03:27

    Try this code:

    var id;
    var vname;
    function ajaxCall(){
    for(var q = 1; q<=10; q++){
     $.ajax({                                            
         url: 'api.php',                        
         data: 'id1='+q+'',                                                         
         dataType: 'json',
         async:false,                    
         success: function(data)          
         {   
            id = data[0];              
            vname = data[1];
         },
        complete: function (data) {
          printWithAjax(); 
         }
        });
    
      }//end of the for statement
      }//end of ajax call function
    

    The "complete" function executes only after the "success" of ajax. So try to call the printWithAjax() on "complete". This should work for you.

    0 讨论(0)
  • 2020-11-30 03:27

    try

    var id;
    var vname;
    function ajaxCall(){
    for(var q = 1; q<=10; q++){
     $.ajax({  
    
     url: 'api.php',                        
     data: 'id1='+q+'',                                                         
     dataType: 'json',
     success: function(data)          
      {
    
        id = data[0];              
       vname = data[1];
       printWithAjax();
    }
          });
    
    
    
    }//end of the for statement
      }//end of ajax call function
    
    0 讨论(0)
  • 2020-11-30 03:29

    Append .done() to your ajax request.

    $.ajax({
      url: "test.html",
      context: document.body
    }).done(function() { //use this
      alert("DONE!");
    });
    

    See the JQuery Doc for .done()

    0 讨论(0)
  • 2020-11-30 03:31

    You can use .ajaxStop() or .ajaxComplete()

    .ajaxComplete() fires after completion of each AJAX request on your page.

    $( document ).ajaxComplete(function() {
      yourFunction();
    });
    

    .ajaxStop() fires after completion of all AJAX requests on your page.

    $( document ).ajaxStop(function() {
      yourFunction();
    });
    
    0 讨论(0)
  • 2020-11-30 03:35

    You should set async = false in head. Use post/get instead of ajax.

    jQuery.ajaxSetup({ async: false });

        $.post({
            url: 'api.php',
            data: 'id1=' + q + '',
            dataType: 'json',
            success: function (data) {
    
                id = data[0];
                vname = data[1];
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题