Skip to content

获取 API

一句话解释

获取 API 是 modern JavaScript 接口 for 制作 HTTP 请求到 retrieve or 发送数据从 服务器.

用大白话说

The Fetch API is the modern way to make HTTP requests in JavaScript. It replaces the older XMLHttpRequest (XHR) with a cleaner, Promise-based interface. You use fetch() to request data from a URL, and it returns a Promise that resolves to the Response object.

A typical fetch call looks like: const res = await fetch(url); const data = await res.json();. The first await gets the response headers, and the second reads and parses the body. You can configure the method (GET, POST, etc.), headers, and body via a second argument.

Fetch only rejects on network errors — HTTP error status codes (404, 500) still resolve. You must check 响应.好的 or 响应.状态 manually to handle HTTP errors.

示例

javascript
async function getUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error("User not found");
  return await res.json();
}

获取 call 那个检查响应.好的 and throws on HTTP 错误, 然后 parses JSON 请求体.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表