axios-miniprogram/docs/pages/basics/response.md

70 lines
1.3 KiB
Markdown
Raw Normal View History

2023-04-15 12:20:09 +08:00
---
title: 响应体
---
# {{ $frontmatter.title }}
::: tip {{ $frontmatter.title }}
2023-04-15 16:21:54 +08:00
`response` 是响应正文。返回的数据类型为 `String/Object/ArrayBuffer`。这取决于请求配置的 `responseType` 属性。
2023-04-15 12:20:09 +08:00
:::
## 通用属性
请求成功返回的 `response` 带有这些属性。
```ts
import axios from 'axios-miniprogram';
axios('https//api.com')
.then((response) => {
const {
// 开发者服务器返回的 HTTP 状态码
status,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 状态文本
statusText,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 开发者服务器返回的数据
data,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 开发者服务器返回的响应头
headers,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 请求配置
config,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 请求任务
request,
} = response;
})
.catch((error) => {
// 失败之后做些什么
});
```
## 平台属性
2023-04-19 12:00:06 +08:00
请求成功返回的 `response` 可能带有平台特有的属性,具体情况取决于平台特性。
2023-04-15 12:20:09 +08:00
微信小程序示例:
```ts
import axios from 'axios-miniprogram';
2023-04-16 00:01:40 +08:00
axios('https://api.com')
2023-04-15 12:20:09 +08:00
.then((response) => {
const {
// 开发者服务器返回的 cookies格式为字符串数组
cookies,
2023-04-15 16:21:54 +08:00
2023-04-15 12:20:09 +08:00
// 网络请求过程中一些调试信息
profile,
} = response;
})
.catch((error) => {
// 失败之后做些什么
});
```
2023-04-15 16:21:54 +08:00
想要了解更多请自行参阅对应平台文档。