- 最終更新
- 2026/08/17
3秒でわかる
ブラウザに標準で入っているHTTP通信の関数。Promiseを返すので、非同期の取得処理を await で直線的に書けるのが利点です。
30秒図解
もう少し詳しく
どういうものか
Fetch API は、fetch(url) をjson() や text() を
なぜ必要か
画面をawait でtry に
具体例
async function loadUser(id) {
const res = await fetch(`/api/users/${id}`, {
headers: { Accept: "application/json" },
});
if (!res.ok) {
throw new Error(`失敗しました status=${res.status}`);
}
return res.json();
}
// 送る側
await fetch("/api/orders", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ itemId: 3, qty: 2 }),
});つまずきやすいところ
404 やres.ok を
似た用語との違い
| 手段 | 特徴 |
|---|---|
| Fetch API | 標準機能で |
| XMLHttpRequest | 古い方式で |
| axios | 外部 |
覚え方
返ってきた


