fetch 是浏览器内置的 HTTP 客户端。它基于 Promise,默认不会因 404/500 自动抛错,需要自己检查 res.ok。

Browser → fetch() → JSON
图:浏览器发起请求到解析 JSON 的流程
const res = await fetch("/api/notes", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ title: "新笔记" }),
});
if (!res.ok) throw new Error(res.status + " " + res.statusText);
const data = await res.json();
提醒:跨域时要关注 CORS;开发阶段可用同源代理简化调试。