[英]Send request to API to receive JSON and append it to the document as raw JSON text
I am trying to fetch the data from randomuser.me/api/
and use the insertAdjacentText method to append it to my blank document.我正在尝试从
randomuser.me/api/
获取数据并使用insertAdjacentText方法将其 append 到我的空白文档中。 So how do I receive data and insert it as adjacent HTML?那么如何接收数据并将其作为相邻的 HTML 插入?
fetch('https://www.randomuser.me/api/')
.then(res => res.json())
.then(data => document.body.insertAdjacentText('afterend', Promise.resolve(data).then(function() { return data; })))
Seems like you are not able to fetch the api result from the promise correctly.似乎您无法正确地从 promise 获取 api 结果。 Wondering below might help.
想知道下面可能会有所帮助。
var d = fetch('https://www.randomuser.me/api/')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(JSON.stringify(data.results[0]));
document.body.insertAdjacentText("afterend",JSON.stringify(data.results[0]));
});
//
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.