Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery多个Ajax请求_Jquery_Json_Ajax - Fatal编程技术网

Jquery多个Ajax请求

Jquery多个Ajax请求,jquery,json,ajax,Jquery,Json,Ajax,我试图从多个ajax请求在JQuery中创建一个数组。这就是我目前所拥有的 $(document).ready(function() { var feedResult = []; var feedCount = []; var displayResources = $("#display-resources"); var arayUrlFederalRegister = [ { source: "FederalRegister:Beef", url : "https://www.fe

我试图从多个ajax请求在JQuery中创建一个数组。这就是我目前所拥有的

$(document).ready(function() {

var feedResult = [];
var feedCount = [];

var displayResources = $("#display-resources");  


var arayUrlFederalRegister = [
{ source: "FederalRegister:Beef", url : "https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=beef&callback=foo"},
{ source: "FederalRegister:Safety", url : "https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=safety&callback=foo"},
{ source: "FederalRegister:Listeria", url : "https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=listeria&callback=foo"} ] ;

function getFederal( $thisSource, $thisURL) {
$.ajax({      
  async: true,
  type: "GET",
  dataType: 'jsonp',
 url: $thisURL,
  success: function(result) {
    var thisCount = 0;
    for (var i in result.results) {
      thisCount++;
      feedResult.push({URL:result.results[i].html_url, title:result.results[i].title, date:result.results[i].publication_date, summary:result.results[i].abstract });
    }
    feedCount.push({source:$thisSource, count:thisCount});  
  }
}); 
};

function pushAllFeeds() {
   $.each( arayUrlFederalRegister, function( index, value ) {
       getFederal(value.source, value.url );
   });
};

function displayFeeds($feedResult) {
 var output = '<div class="bsr-results well">';
 $.each( $feedResult, function( index, value ) {
      output +=
        '<h3><a href="' +
        value.URL +
        '" target="_blank">' +
        value.title +
        '</a></h3><div><div class="bsr-date">' +
        value.date +
        '</div><p>' +
        value.abstract +
        '</p></div><hr>';
    });
    output += "</div>";
    displayResources.html(output);      
};


$.when(pushAllFeeds()).then(function() {
 console.log(feedResult);
 displayFeeds(feedResult);
});

});
$(文档).ready(函数(){
var feedResult=[];
var feedCount=[];
var displayResources=$(“#显示资源”);
var Arayurl联邦登记员=[
{来源:“联邦登记员:牛肉”,网址:https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=beef&callback=foo"},
{来源:“联邦登记员:安全”,url:https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=safety&callback=foo"},
{来源:“联邦登记员:李斯特菌”,网址:https://www.federalregister.gov/api/v1/documents.json?fields%5B%5D=abstract&fields%5B%5D=html_url&fields%5B%5D=publication_date&fields%5B%5D=title&per_page=2&order=newest&conditions%5Bterm%5D=listeria&callback=foo"} ] ;
函数getFederal($thisSource,$thisURL){
$.ajax({
async:true,
键入:“获取”,
数据类型:“jsonp”,
url:$thisURL,
成功:功能(结果){
var-thiscont=0;
for(result.results中的var i){
thisCount++;
feedResult.push({URL:result.results[i].html\URL,标题:result.results[i]。标题,日期:result.results[i]。发布日期,摘要:result.results[i]。摘要});
}
push({source:$thisSource,count:thisCount});
}
}); 
};
函数pushAllFeeds(){
$.each(arayUrlFederalRegister,函数(索引,值){
getFederal(value.source,value.url);
});
};
函数displayFeeds($feedResult){
var输出=“”;
$。每个($feedResult,函数(索引,值){
输出+=
'' +
value.date+
“”+
价值观+
“


”; }); 输出+=“”; html(输出); }; $.when(pushAllFeeds()).then(function()){ 控制台日志(feedResult); 显示feeds(feedResult); }); });

控制台正在显示正确构建的feedResult数组(6个对象),但是displayFeeds函数没有用作用于初始空数组的任何内容填充#display resources div元素。在完成所有ajax请求并完成feedResult数组后,如何将参数传递给displayFeeds()函数?谢谢。

您不能只调用$。在使用任何函数时,都必须根据承诺调用它。pushAllFeeds不会返回承诺,因此它不会在调用.then回调之前等待它。谢谢,我现在正在记录.promise上的内容。如果我没有在pushAllFeeds()的末尾调用$。那么console.log(feedResults)也是空的,所以我认为这不是完全错误的。