api - 将 fetch() 与 API 一起使用
问题描述
我尝试使用 fetch() 从https://swapi.co/获取数据 使用此代码我得到未定义但在 chrome 的“网络”部分中我看到了我需要的数据。我怎样才能访问它?
fetch('https://swapi.co/api/people/1/')
.then(resp => resp.json)
.then(obj => console.log(obj));
解决方案
您好,这将获取以 json 形式返回的数据
fetch('https://swapi.co/api/people/1')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
推荐阅读
- android - Firestore 中的布尔值通过复选框切换。需要点击 2 次才能更改值?
- php - 尝试使用 phpmailer 和 g-suite 发送电子邮件
- javascript - 网上搜索js库参考
- jestjs - 我可以让 jest 串行运行 Javascript 测试吗?
- css - 如何扩展 FontAwesome 图标
- java - Hackerrank (Java) MAXIMUM LENGTH SUBSTRING - 超时错误
- android - 如何在日历视图中禁用未来日期
- class - 处理对象中计算属性的正确方法是什么?
- fortran - 为什么在 Fortran 上进行黎曼和近似时,中点规则比辛普森规则更准确
- python - 尝试从 selenium 使用 webdriver 时出错