mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
13fdbec872
* chore: update changelog and update gitignore * chore: update PR and issue templates * chore: updated docs * chore: update all docs to match * chore: update both files to match * chore: remove un-needed yml * Update docs/fr/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/es/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/es/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/fr/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
123 lines
3.3 KiB
Markdown
123 lines
3.3 KiB
Markdown
# 入门指南
|
||
|
||
欢迎阅读 axios 文档!本指南将帮助你快速上手 axios,并发起第一个 API 请求。如果你是 axios 新手,建议从这里开始。
|
||
|
||
## 安装
|
||
|
||
你可以通过多种方式在项目中使用 axios。最常见的方式是通过 npm 安装,也支持 jsDelivr、unpkg 等 CDN。
|
||
|
||
#### 使用 npm
|
||
|
||
```bash
|
||
npm install axios
|
||
```
|
||
|
||
#### 使用 pnpm
|
||
|
||
```bash
|
||
pnpm install axios
|
||
```
|
||
|
||
#### 使用 yarn
|
||
|
||
```bash
|
||
yarn add axios
|
||
```
|
||
|
||
#### 使用 bun
|
||
|
||
```bash
|
||
bun add axios
|
||
```
|
||
|
||
#### 使用 deno
|
||
|
||
```bash
|
||
deno install npm:axios
|
||
```
|
||
|
||
#### 使用 jsDelivr
|
||
|
||
使用 jsDelivr 时,建议使用压缩版本并固定版本号,以避免意外更新。如需使用最新版本,可以去掉版本号,但强烈不建议在生产环境这样做,因为可能导致应用出现意外变化。
|
||
|
||
```html
|
||
<script src="https://cdn.jsdelivr.net/npm/axios@<x.x.x>/dist/axios.min.js"></script>
|
||
```
|
||
|
||
#### 使用 unpkg
|
||
|
||
使用 unpkg 时,建议使用压缩版本并固定版本号,以避免意外更新。如需使用最新版本,可以去掉版本号,但强烈不建议在生产环境这样做,因为可能导致应用出现意外变化。
|
||
|
||
```html
|
||
<script src="https://unpkg.com/axios@<x.x.x>/dist/axios.min.js"></script>
|
||
```
|
||
|
||
## 导入 axios
|
||
|
||
安装完成后,你可以使用 `import` 或 `require` 来导入此库:
|
||
|
||
```js
|
||
import axios, { isCancel, AxiosError } from "axios";
|
||
```
|
||
|
||
也可以使用默认导出,因为命名导出只是从 axios 工厂的再导出:
|
||
|
||
```js
|
||
import axios from "axios";
|
||
|
||
console.log(axios.isCancel("something"));
|
||
```
|
||
|
||
如果使用 `require` 导入,**只有默认导出可用**:
|
||
|
||
```js
|
||
const axios = require("axios");
|
||
|
||
console.log(axios.isCancel("something"));
|
||
```
|
||
|
||
某些打包器和 ES6 lint 规则可能需要:
|
||
|
||
```js
|
||
import { default as axios } from "axios";
|
||
```
|
||
|
||
对于自定义或较旧的环境,如果模块解析行为不正常,可以直接导入预构建包:
|
||
|
||
```js
|
||
const axios = require("axios/dist/browser/axios.cjs"); // 浏览器 CommonJS 包(ES2017)
|
||
// const axios = require("axios/dist/node/axios.cjs"); // node CommonJS 包(ES2017)
|
||
```
|
||
|
||
## 发起第一个请求
|
||
|
||
使用 axios 发起请求最少只需要两行代码。你可以通过提供 URL 和请求方法向任意 API 发送请求。例如,向 JSONPlaceholder API 发起一个 GET 请求:
|
||
|
||
```js
|
||
import axios from "axios";
|
||
|
||
const response = await axios.get(
|
||
"https://jsonplaceholder.typicode.com/posts/1"
|
||
);
|
||
|
||
console.log(response.data);
|
||
```
|
||
|
||
axios 提供了简洁的请求 API。你可以使用 `axios.get` 发起 GET 请求,使用 `axios.post` 发起 POST 请求,依此类推。也可以使用 `axios.request` 方法发起任意类型的请求。
|
||
|
||
::: tip 在生产环境中设置 `timeout`
|
||
如果不设置 `timeout`,停滞的请求可能会无限挂起。可通过请求配置传入:
|
||
|
||
```js
|
||
const response = await axios.get("https://example.com/data", {
|
||
timeout: 5000, // 5 秒
|
||
});
|
||
```
|
||
|
||
匹配的 `ECONNABORTED` / `ETIMEDOUT` 错误码请参阅[请求配置中的 `timeout`](/pages/advanced/request-config#timeout) 与[错误处理](/pages/advanced/error-handling)。
|
||
:::
|
||
|
||
## 下一步
|
||
|
||
现在你已经用 axios 完成了第一个请求,可以继续探索 axios 文档的其余内容。了解更多关于发起请求、处理响应以及在项目中使用 axios 的知识,请查阅文档其他章节。
|