2016-11-17 18 views
1

我正在使用两个AJAX GET请求从两个不同的来源获取数据。
在每个请求中我分析响应数据,使用相关数据创建一个临时数组,并将这个临时数组推送到主数组中。
但是,只有当我在AJAX请求中包含"async: false"时才起作用,现在不推荐使用该请求。
有没有其他的方法可以写这段代码? (注意:尽管我在这段代码中使用了两个AJAX请求,但我可能需要在我正在处理的项目中总共使用四个请求)。连续AJAX调用的替代“异步:false”

function get_results() { 
    $(document).ready(function() { 
    var master_array = []; 
    $.ajax({ 
     type: "GET", 
     url: "http//:www.source1.com", 
     dataType: "xml", 
     async: false, 
     success: function(xml) { 
     $(xml).find('product').each(function() { 
      var Average = $(this).find('Average').text(); 
      var Price = $(this).find('Price').text(); 
      var Name = $(this).find('Name').text(); 
      var Url = $(this).find('Url').text(); 
      var Image = $(this).find('Image').text(); 
      master_array.push([Average, Price, Name, Url, Image]); 
     }); 
     } 
    }); 
    $.ajax({ 
     type: "GET", 
     url: "http//:www.source2.com", 
     dataType: "xml", 
     async: false, 
     success: function(xml) { 
     $(xml).find('product').each(function() { 
      var Average = $(this).find('Average').text(); 
      var Price = $(this).find('Price').text(); 
      var Name = $(this).find('Name').text(); 
      var Url = $(this).find('Url').text(); 
      var Image = $(this).find('Image').text(); 
      master_array.push([Average, Price, Name, Url, Image]); 
     }); 
     } 
    }); 
    }); 
} 
+0

你所做的是将第二个请求移动到第一个请求的成功函数的末尾。你也可以使用Promise。 –

+0

把你的第二个Ajax请求放在第一个Ajax成功回调结束时 –

回答

2

您可以通过使用JQuery $.promise对象来实现此目的。
当你return $.ajax它实际上有一些内置的方法,如 $.done()$.when

在您的场景中,您希望在第一个Ajax请求完成之后执行Ajax功能。

所以我们将创建两个变量来表示您在代码中的两个Ajax请求。
就像我之前提到的那样,当你返回Ajax请求时,你实际上可以将它用作$.promise对象并享受它的优点,比如$.done()函数。

function get_results() { 
    $(document).ready(function() { 
    var master_array = []; 

    var MyFirstFunction = function() { 
     return $.ajax({ 
     type: "GET", 
     url: "http//:www.source1.com", 
     dataType: "xml", 
     success: function(xml) { 
     $(xml).find('product').each(function() { 
      var Average = $(this).find('Average').text(); 
      var Price = $(this).find('Price').text(); 
      var Name = $(this).find('Name').text(); 
      var Url = $(this).find('Url').text(); 
      var Image = $(this).find('Image').text(); 
      master_array.push([Average, Price, Name, Url, Image]); 
     }); 
     } 
    }); 
    }; 

    var MySecondFunction = function(){ 
     return $.ajax({ 
     type: "GET", 
     url: "http//:www.source2.com", 
     dataType: "xml", 
     success: function(xml) { 
     $(xml).find('product').each(function() { 
      var Average = $(this).find('Average').text(); 
      var Price = $(this).find('Price').text(); 
      var Name = $(this).find('Name').text(); 
      var Url = $(this).find('Url').text(); 
      var Image = $(this).find('Image').text(); 
      master_array.push([Average, Price, Name, Url, Image]); 
     }); 
     } 
    }); 
    }; 
//We use it after the assignment of the variables because if would have put it before them we would got undefined, you can read more about it here:[Hoisting][4]. 
     MyFirstFunction().done(MySecondFunction); 
    }); 
    } 
+3

没有评论的投票没有帮助任何人。 –