<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fetch基础用法</title>
</head>
<body>
<div id="container"></div>
<script>
async function getData() {
let response = await fetch("http://jsonplaceholder.typicode.com/posts");
let posts = await response.json();
let container = document.getElementById("container");
let ol = document.createElement("ol");
posts.forEach(post => {
let li = document.createElement("li");
let anchor = document.createElement("a");
anchor.appendChild(document.createTextNode(post.title));
anchor.setAttribute("href",`http://jsonplaceholder.typicode.com/posts/${post.id}`);
li.appendChild(anchor);
ol.appendChild(li);
});
container.appendChild(ol);
}
getData();
</script>
</body>
</html>
fetchAPI基本用法