Api请求返回的是URL而不是数据

时间:2017-08-10 08:20:17

标签: jquery api

使用api我用来获取第三个LI的信息它只返回我的信息网址。我如何在同一个电话中实际访问该网址中的信息?



var $orders = $('#lukeData');
$.ajax({
  type: 'GET',
  url: "https://swapi.co/api/people/1/",
  success: function(data) {
    $.each(data, function(i, order) {
      $orders.html('<ul><b>Sex:' + data.gender + '<br>' + data.birth_year + '<br>' + data.starships + '</b></ul>');
    });
  }
})
&#13;
#boxOne {
  width: 300px;
  height: 300px;
  text-align: center;
  background-position: center;
  background-image: url();
  position: relative;
  left: 50%;
  right: 50;
  margin-top: 50px;
  margin-left: -400px;
  border-radius: 10px;
  padding: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<div id="boxOne" class="container">
  <h1>Luke SkyWalker</h1>
  <div id="lukeData"> Details</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试加载person然后获取其sharship(s)的详细信息 - 您需要为每个星舰进行更多API请求 - 例如下面的内容。

&#13;
&#13;
var $orders = $('#lukeData');
$.ajax({
  type: 'GET',
  url: "https://swapi.co/api/people/1/?format=json"
}).then(function(data){
  var html = '<b>Sex:' + data.gender + '<br>' + data.birth_year;
  
  $.when(...data.starships.map(function(ssUrl){
      return $.ajax({
         type:'get',
         url:ssUrl + "?format=json"
      })
  })).then(function(...starships){
      html += "<ul>";
      starships.forEach(function(ss){
          html += "<li>" + ss[0].name + "</li>";
      });
      html += "</ul>"
      $orders.html(html);
  });
});
&#13;
#boxOne {
  width: 300px;
  height: 300px;
  text-align: center;
  background-position: center;
  background-image: url();
  position: relative;
  left: 50%;
  right: 50;
  margin-top: 50px;
  margin-left: -400px;
  border-radius: 10px;
  padding: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<div id="boxOne" class="container">
  <h1>Luke SkyWalker</h1>
  <div id="lukeData"> Details</div>
</div>
&#13;
&#13;
&#13;