首页 > 解决方案 > 将 fetch() 与 API 一起使用

问题描述

我尝试使用 fetch() 从https://swapi.co/获取数据 使用此代码我得到未定义但在 chrome 的“网络”部分中我看到了我需要的数据。我怎样才能访问它?

fetch('https://swapi.co/api/people/1/')
.then(resp => resp.json)
.then(obj => console.log(obj));

标签: apifetch

解决方案


您好,这将获取以 json 形式返回的数据

fetch('https://swapi.co/api/people/1')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  });


推荐阅读