从多个API获取数据

时间:2018-12-02 23:41:10

标签: javascript jquery ajax api

我需要从多个API提取数据并将其显示在表中。 我想到的是:

var req = $.ajax({
  url: "....linkhere",

  dataType: 'jsonp'
});
var req = $.ajax({
  url: "....linkhere1",

  dataType: 'jsonp'
});

req.done(function(data) {
  console.log(data);
  var infoTable = $("<table />");

  var arrayL = data.length;
  var outputString = '';

  for (var i = 0; i < arrayL; i++) {
    var tableRow = $("<tr/>");

    titleString = data[i].title;
    var titleCell = $("<td />", {
      text: titleString
    });
    detailString = data[i].description;
    var detailCell = $("<td/>", {
      text: detailString
    });

    tableRow.append(titleCell).append(detailCell);
    infoTable.append(tableRow);
  }

  $("#display-resources").append(infoTable);
});
});

尽管如此,我只能从一个api获取数据。如何从多个中提取?

编辑:

我正在尝试添加文本输入,因此我可以发送有关特定单词的请求。我试图用新结果附加现有表。我正在尝试更改下面作为答案提供的代码。但是,我做了一些工作,但没有成功。

$("#inputChoice").on("blur", function() {
    let choice = $(this).val();
    let req = $.ajax({
        url: "...APIlink"+choice,
        dataType: "jsonp"
    });
    req.done(function (data) {
        console.log(data);
        var infoTable = $("<table />");

        let arrayL = data.length;
        for (var i = 0; i < arrayL; i++) {
            var tableRow = $("<tr/>");

            titleString = data[i].title;
            var titleCell = $("<td />", {
                text: titleString
            });
            titleCell.addClass("title-row");


            detailString = data[i].description;
            var detailCell = $("<td/>", {
                text: detailString


            });
            detailCell.addClass("details-row")

            tableRow.append(titleCell).append(detailCell);
            infoTable.append(tableRow);
        }
        $("#display-resources").append(infoTable);
});
});

1 个答案:

答案 0 :(得分:2)

从端点请求数据,当它们全部完成后,创建一个表

function multiReq(...links) {
  let responseCount = links.length;
  const responses = [];
  let handler;
  function responseHandler(i) {
    return data => {
      responseCount -= 1;
      responseCount === 0
        ? handler([].concat(...responses))
        : (responses[i] = data)
    }
  }
  links.forEach((link, i) => $.ajax({
    url: link,
    dataType: 'jsonp'
  }).done(responseHandler(i)));
  return {
    done(callback) {
      handler = callback;
    }
  };
}


multiReq(link1, link2).done((data) => {})

function multiReq(...links) {
  return Promise.all(links.map(link => $.ajax({
    url: link,
    dataType: 'jsonp'
  }))).then((...responses) => [].concat(...responses))
}


multiReq(link1, link2).then(data => {
    // create table
})

function multiReq(...links) {
  return $.when(...links.map(link => $.ajax({
    url: link,
    dataType: 'jsonp'
  }))).then((...responses) => [].concat(...responses.map(([data]) => data)))
}


multiReq(link1, link2).done(data => {
    // create table
})

或尽可能接近您的代码:

var req1 = $.ajax({
  url: "https://wt-65edf5a8bfb22e61214c31665c92dbd2-0.sandbox.auth0-extend.com/link-1",

  //dataType: 'jsonp'
});
var req2 = $.ajax({
  url: "https://wt-65edf5a8bfb22e61214c31665c92dbd2-0.sandbox.auth0-extend.com/link-1",

  //dataType: 'jsonp'
});

$.when(req1, req2).done(function([data1], [data2]) {
  var data = data1.concat(data2); // merge data from both request into one
  
  //console.log(data);
  var infoTable = $("<table />");

  var arrayL = data.length;
  var outputString = '';

  for (var i = 0; i < arrayL; i++) {
    var tableRow = $("<tr/>");

    titleString = data[i].title;
    var titleCell = $("<td />", {
      text: titleString
    });
    detailString = data[i].description;
    var detailCell = $("<td/>", {
      text: detailString
    });

    tableRow.append(titleCell).append(detailCell);
    infoTable.append(tableRow);
  }

  $("#display-resources").append(infoTable);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="display-resources"></div>