fetch 串 PokeAPI
fetch("https://pokeapi.co/api/v2/pokemon/ditto")
.then((res) => {
return res.json(); // 解析 JSON 數據
})
.then((data) => {
console.log(data); // 輸出解析後的數據
renderData(data); // 渲染數據到 HTML
})
.catch((error) => {
console.error("Error fetching data:", error); // 處理錯誤
});
function renderData(data) {
const container = document.getElementById("pokemon-container");
const html = `
<h1>${data.name}</h1>
<p>Height: ${data.height}</p>
<p>Weight: ${data.weight}</p>
<p>Base Experience: ${data.base_experience}</p>
`;
container.innerHTML = html;
}
<div id="pokemon-container"></div>