Fetch API
Fetch API 用于在浏览器中发送 HTTP 请求,适合 JSON 接口、文件上传和流式响应场景。
语法
fetch(input, init)
最小示例
const res = await fetch('/api/posts');
const data = await res.json();
常见错误
- 只看语法不看运行环境限制。
- 没有处理错误、权限或兼容性分支。
- 示例上线前应结合真实浏览器、服务器或 Git 仓库验证。
Fetch API 用于在浏览器中发送 HTTP 请求,适合 JSON 接口、文件上传和流式响应场景。
fetch(input, init)
const res = await fetch('/api/posts');
const data = await res.json();