Deferred and Ajax

后端 未结 3 885
生来不讨喜
生来不讨喜 2021-02-06 06:33

Reading a JSON-Service like this:

$.ajax({
  url:\'activeIDs\',
  success : function(data){ // data = [14,15]
    var tableRows = [];
    for (var dataIndex=0; d         


        
3条回答
  •  悲&欢浪女
    2021-02-06 07:18

    Use jQuery when

    // Function to return id info based on this example http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success 
    function getInfo(ID) {
    return $.get("info?id=" + ID);  
    }
    // based on examples from https://api.jquery.com/jQuery.when/
    var promises = [];
    var jqxhr = $.get("activeIDs")
      .done(function(data) {
        $.each(data,function(i,v) {
            promises.push(getInfo(v));
        });
        $.when(promises).done(function(p) {
            console.log(p,p.length);
        });
      })
      .fail(function() {
        alert( "Error" );
      });
    

提交回复
热议问题