fetch() のステップ別実行デモ
JavaScript コード
const response = await fetch(url);
log("ステータス: " + response.status);
log("Content-Type: " + response.headers.get("Content-Type"));
const profile = await response.json();
log("→ プロフィール表示: " + profile.name);
log("→ プロフィール表示: " + profile.email);
log("→ プロフィール表示: " + profile.age);
catch (err) { log("エラー発生: " + err.message); }
finally { log("通信処理完了"); }
PHP コード(user_profile.php)
<?php
sleep(5);
if (isset($_GET['raw'])) {
header('Content-Type: text/plain');
echo "これはJSONではありません";
exit;
}
header('Content-Type: application/json');
echo json_encode([
"name" => "増田健人",
"email" => "masuda@example.com",
"age" => 28
]);
?>