JavaScript,获取,API
我想做以下操作:从此网站获取随机名称 https://swapi.dev/api /people/,我这样做了,我可以在HTML页面中看到它,然后我也想获得一个随机的星球,在这里我需要访问HomeWorld键,并返回链接,然后再返回链接i i格式化以获取一个随机的URL,从该网址中,我还必须在页面上显示该星球的名称。第一个获取效果很好,至少我认为,但是第三个。然后,然后是不起作用的,或者至少我不知道如何从HomeWorld URL访问信息。这是我第一次尝试
,如果您可以帮助我告诉我在代码和不同的解决方案中做错了什么,但并非如此复杂,那将是很好的。
let randomNumber = Math.floor(Math.random()*9)
const fetchPromise = fetch("https://swapi.dev/api/people/");
let test
let test2
let planets = document.querySelector('#age')
fetchPromise
.then((response) => {
if (!response.ok) {
throw new Error(`Http error: ${response.status}`);
}
return response.json();
})
.then((json) => {
console.log(json.results[randomNumber].name)
showRandomUserData(json)
test = json.results[0].homeworld
test = test.slice(0, -2)
// console.log(test + randomNumber + "/");
// console.log(test + "/" + randomNumber + "/");
test = test + randomNumber + "/";
return fetch(test)
// return fetch("https://swapi.dev/api/planets/2/");
})
.then(response => response.json()).then(json =>
{ test2=json.name
console.log(test2);
planets.innerHTML = test2
})
showRandomUserData = (randomUser) => {
document.querySelector("#name").innerHTML =
randomUser.results[randomNumber].name;
}
已解决
I want to do the following: get a random name with fetch from this website https://swapi.dev/api/people/, which I did and I can see it in my html page then I want also to get a random planet, here I need to access the homeworld key, and to return the link, before returning the link I formatted to get a random URL and from this one I also have to show the name of the planet on my page. The first fetch works fine, at least I think but the third .then()
is not working or at least I don't know how to access the information from the homeworld URL. This is my first time trying fetch()
and it will be nice if you can help me telling where I did wrong in code and maybe different solutions but not so complicated.
let randomNumber = Math.floor(Math.random()*9)
const fetchPromise = fetch("https://swapi.dev/api/people/");
let test
let test2
let planets = document.querySelector('#age')
fetchPromise
.then((response) => {
if (!response.ok) {
throw new Error(`Http error: ${response.status}`);
}
return response.json();
})
.then((json) => {
console.log(json.results[randomNumber].name)
showRandomUserData(json)
test = json.results[0].homeworld
test = test.slice(0, -2)
// console.log(test + randomNumber + "/");
// console.log(test + "/" + randomNumber + "/");
test = test + randomNumber + "/";
return fetch(test)
// return fetch("https://swapi.dev/api/planets/2/");
})
.then(response => response.json()).then(json =>
{ test2=json.name
console.log(test2);
planets.innerHTML = test2
})
showRandomUserData = (randomUser) => {
document.querySelector("#name").innerHTML =
randomUser.results[randomNumber].name;
}
Solved
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个简单的解决方案,它使用
fetch()
从这两个URL中获取数据,然后插入所有返回到网页的人和一个星球:至于使用结果,人们URL返回一个看起来像这样的结构:
因此,您有
people.results
,这是一个数组,您可以访问people.results [n]代码>从该数组中获取项目。 的对象。
该项目将是一个
具有
.name
,的属性 您可以在planet.name
中访问该对象上的属性。请注意,人们的结果是分页的。总共有82个,但第一个结果只有10个。其余的带有其他页面的结果,例如
https://swapi.dev/api/people/?page=2
。Here's a simple solution that uses
fetch()
to grab data from both those URLs and then insert all the people and the one planet that is returned into your web page:As for using the results, the people URL returns a structure that looks like this:
So, you have
people.results
which is an array and you can accesspeople.results[n]
to get an item from that array. That item will be an object which has properties like.name
,.height
, etc...The specific planet URL you show returns a single planet object like this:
So, you access properties on that object as in
planet.name
.Notice that the people results are paged. There are 82 total results, but only 10 come in this first result. The rest come with results for other pages such as
https://swapi.dev/api/people/?page=2
.类似于这个答案但是使用async/等待避免回调地狱。如果可以,请尝试使用此方法。 为什么
? href =“ https://stackoverflow.com/users/816620/jfriend00”> jfriend00 使用
Promise> Promise.all
,而不是单独的fetch呼叫,因为这使得获取可以并行发生。 了解更多。sandbox 进行测试并尝试
Similar to this answer but using async/await to avoid callback hell. If you can, try using this approach. Why?
Excellent recommendation in that answer by jfriend00 to use
Promise.all
instead of separate fetch calls, as that enables fetching to happen in parallel. To know more.sandbox to test and try