weixin_33720186 2018-03-12 02:22 采纳率: 0%
浏览 49

如何从 URL 中获取值?

我使用jQuery从以下API获取数据 (https://swapi.co/api/people/)。

到目前为止,我可以得到所有的主要数据,“名称”、“高度”、“质量”等等。但是,当我试图得到“主世界”、“电影”、“物种”、“车辆”、“星舰”的值时却只显示它所指的网址,我不知道该用什么方法在这些URL中获取值。

我的代码:

$(document).ready(function() {
  function getStarWars() {
    $.ajax({
      dataType: 'json',
      type: 'GET',
      async: true,
      url: 'https://swapi.co/api/people/1/',
      success: function(datos) {
        if (datos != null) {
          console.log(datos.name);
          console.log(datos.homeworld);
          console.log(datos.films);
          console.log(datos.species);
        }
      },
      error: function(xhr, status) {
        alert('Sorry, i have a problem');
      }
    });
  }

  getStarWars();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</div>
  • 写回答

2条回答 默认 最新

  • 游.程 2018-03-12 02:50
    关注

    You need to re-fetch the data of that new link

    $(document).ready(function() {
      function getStarWars() {
        $.ajax({
          dataType: 'json',
          type: 'GET',
          async: true,
          url: 'https://swapi.co/api/people/1/',
          success: function(datos) {
            if (datos != null) {
              console.log(datos.name);
              console.log(datos.homeworld, 'asdasdasd');
              console.log(datos.films);
              console.log(datos.species);
              getStarWarsNew(datos.homeworld)
            }
    
          },
          error: function(xhr, status) {
            alert('Sorry, i have a problem');
          }
        });
      }
    
      function getStarWarsNew(URL) {
        $.ajax({
          dataType: 'json',
          type: 'GET',
          async: true,
          url: URL,
          success: function(datos) {
            if (datos != null) {
              console.log(datos);
    
            }
    
          },
          error: function(xhr, status) {
            alert('Sorry, i have a problem');
          }
        });
      }
    
      getStarWars();
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路