Skip to content

Commit bc0f11f

Browse files
Updated Simplified Chinese intro topic; Translate en doc to Simplified Chinese (axios#116)
* [Added] Translate multipart topic to Simplified Chinese * [Updated] Added `multipart` topic; [Updated] `url-encoded` topic; * Updated Simplified Chinese intro topic; * [Updated] Added `multipart` topic; [Updated] `url-encoded` topic; [Added] http server script; * [Updated] Added docs; Updated Simplified Chinese intro topic; [Updated] `intro` topic; [Updated] `api_intro` topic; [Updated] `post_example` topic; --------- Co-authored-by: Jay <[email protected]>
1 parent 35e9b4f commit bc0f11f

File tree

8 files changed

+295
-11
lines changed

8 files changed

+295
-11
lines changed

posts/zh/api_intro.md

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ axios('/user/12345');
5454
##### axios.post(url[, data[, config]])
5555
##### axios.put(url[, data[, config]])
5656
##### axios.patch(url[, data[, config]])
57+
##### axios.postForm(url[, data[, config]])
58+
##### axios.putForm(url[, data[, config]])
59+
##### axios.patchForm(url[, data[, config]])
5760

5861
###### 注意
5962
在使用别名方法时, `url``method``data` 这些属性都不必在配置中指定。

posts/zh/cancellation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: '取消请求'
33
prev_title: '错误处理'
44
prev_link: '/zh/docs/handling_errors'
5-
next_title: 'URL-Encoding Bodies'
5+
next_title: '请求体编码'
66
next_link: '/zh/docs/urlencoded'
77
---
88

posts/zh/intro.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ Axios 是一个基于 *[promise](https://javascript.info/promise-basics)* 网络
1616
- 拦截请求和响应
1717
- 转换请求和响应数据
1818
- 取消请求
19+
- 超时处理
20+
- 查询参数序列化支持嵌套项处理
21+
- 自动将请求体序列化为:
22+
- JSON (`application/json`)
23+
- Multipart / FormData (`multipart/form-data`)
24+
- URL encoded form (`application/x-www-form-urlencoded`)
25+
- 将 HTML Form 转换成 JSON 进行请求
1926
- 自动转换JSON数据
27+
- 获取浏览器和 node.js 的请求进度,并提供额外的信息(速度、剩余时间)
28+
- 为 node.js 设置带宽限制
29+
- 兼容符合规范的 FormData 和 Blob(包括 node.js)
2030
- 客户端支持防御[XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery)
2131

2232
# 安装
@@ -49,4 +59,11 @@ $ yarn add axios
4959

5060
```html
5161
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
52-
```
62+
```
63+
64+
为了直接使用 `require` 导入预构建的 CommonJS 模块(如果您的模块打包器无法自动解析它们),我们提供了以下预构建模块:
65+
66+
```js
67+
const axios = require('axios/dist/browser/axios.cjs'); // browser
68+
const axios = require('axios/dist/node/axios.cjs'); // node
69+
```

posts/zh/multipart.md

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
title: 'Multipart 实体请求'
3+
prev_title: '请求体编码'
4+
prev_link: '/zh/docs/urlencoded'
5+
next_title: '注意事项'
6+
next_link: '/zh/docs/notes'
7+
---
8+
9+
## 使用 `multipart/form-data` 类型发起 `POST` 请求
10+
11+
### 使用 FormData API
12+
13+
#### 浏览器
14+
15+
```js
16+
const form = new FormData();
17+
form.append('my_field', 'my value');
18+
form.append('my_buffer', new Blob([1,2,3]));
19+
form.append('my_file', fileInput.files[0]);
20+
21+
axios.post('https://example.com', form)
22+
```
23+
24+
Axios 会将传入数据序列化,因此使用 Axios 提供的 API 可以无需手动处理 FormData 的数据并实现一样的效果:
25+
26+
```js
27+
axios.postForm('https://httpbin.org/post', {
28+
my_field: 'my value',
29+
my_buffer: new Blob([1,2,3]),
30+
my_file: fileInput.files // FileList will be unwrapped as sepate fields
31+
});
32+
```
33+
34+
HTML 表单可以直接作为请求内容来进行传输。
35+
36+
#### Node.js
37+
38+
```js
39+
import axios from 'axios';
40+
41+
const form = new FormData();
42+
form.append('my_field', 'my value');
43+
form.append('my_buffer', new Blob(['some content']));
44+
45+
axios.post('https://example.com', form)
46+
```
47+
48+
由于 node.js 当前不支持从文件创建 `Blob`,因此您可以使用第三方软件包来实现该目的。
49+
50+
```js
51+
import {fileFromPath} from 'formdata-node/file-from-path'
52+
53+
form.append('my_field', 'my value');
54+
form.append('my_file', await fileFromPath('/foo/bar.jpg'));
55+
56+
axios.post('https://example.com', form)
57+
```
58+
59+
当 Axios 版本小于 `v1.3.0` 时您必须引入 `form-data` 包。
60+
61+
```js
62+
const FormData = require('form-data');
63+
64+
const form = new FormData();
65+
form.append('my_field', 'my value');
66+
form.append('my_buffer', new Buffer(10));
67+
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
68+
69+
axios.post('https://example.com', form)
70+
```
71+
72+
### 🆕 自动序列化
73+
74+
`v0.27.0` 版本开始,当请求头中的 Content-Type 是 `multipart/form-data` 时,Axios 支持自动地将普通对象序列化成一个 FormData 对象。
75+
76+
这个示例请求演示了如何将一个数据通过 `FormData` 格式进行提交(浏览器与 Node.js 环境):
77+
78+
```js
79+
import axios from 'axios';
80+
81+
axios.post('https://httpbin.org/post', {
82+
user: {
83+
name: 'Dmitriy'
84+
},
85+
file: fs.createReadStream('/foo/bar.jpg')
86+
}, {
87+
headers: {
88+
'Content-Type': 'multipart/form-data'
89+
}
90+
}).then(({data})=> console.log(data));
91+
```
92+
93+
Axios FormData 序列化器支持一些特殊的结尾,以执行以下操作:
94+
95+
- `{}` - 通过 `JSON.stringify` 序列化数据
96+
- `[]` - 将 array-like 的对象使用相同的键值来展开为单独的字段
97+
98+
> 提示:默认情况下,展开、扩展操作将在数组和 FileList 对象上使用。
99+
>
100+
101+
FormData 序列化器支持通过 `config.formSerializer: object` 这个参数来传递一些额外的选项,以支持一些特殊的情况:
102+
103+
- `visitor: Function` - 用户定义的处理函数,将递归调用以按照自定义规则将数据对象序列化为`FormData`对象。
104+
- `dots: boolean = false` - 使用点符号而不是括号来序列化数组和对象;
105+
- `metaTokens: boolean = true` - 在 FormData 键值中添加特殊结尾(例如`user{}: '{"name": "John"}'`)。后端的 body-parser 可能会使用此元信息自动将值解析为 JSON。
106+
- `indexes: null|false|true = false` - 控制如何添加索引到打平的 array-like 对象的展开键值中
107+
- `null` - 不添加中括号(`arr: 1``arr: 2``arr: 3`
108+
- `false`(默认值)- 添加空中括号(`arr[]: 1``arr[]: 2``arr[]: 3`
109+
- `true` - 添加带有索引的中括号(`arr[0]: 1``arr[1]: 2``arr[2]: 3`
110+
111+
假设说我们有一个这样的示例对象:
112+
113+
```js
114+
const obj = {
115+
x: 1,
116+
arr: [1, 2, 3],
117+
arr2: [1, [2], 3],
118+
users: [{name: 'Peter', surname: 'Griffin'}, {name: 'Thomas', surname: 'Anderson'}],
119+
'obj2{}': [{x:1}]
120+
};
121+
```
122+
123+
接下来这些序列化的步骤将会由 Axios 内置的序列化器自动执行:
124+
125+
```js
126+
const formData= new FormData();
127+
formData.append('x', '1');
128+
formData.append('arr[]', '1');
129+
formData.append('arr[]', '2');
130+
formData.append('arr[]', '3');
131+
formData.append('arr2[0]', '1');
132+
formData.append('arr2[1][0]', '2');
133+
formData.append('arr2[2]', '3');
134+
formData.append('users[0][name]', 'Peter');
135+
formData.append('users[0][surname]', 'Griffin');
136+
formData.append('users[1][name]', 'Thomas');
137+
formData.append('users[1][surname]', 'Anderson');
138+
formData.append('obj2{}', '[{"x":1}]');
139+
```
140+
141+
```js
142+
import axios from 'axios';
143+
144+
axios.post('https://httpbin.org/post', {
145+
'myObj{}': {x: 1, s: "foo"},
146+
'files[]': document.querySelector('#fileInput').files
147+
}, {
148+
headers: {
149+
'Content-Type': 'multipart/form-data'
150+
}
151+
}).then(({data})=> console.log(data));
152+
```
153+
154+
Axios支持以下别名方法:`postForm``putForm``patchForm`,这些方法只是对应的 HTTP 方法,其 content-type 头部默认设为`multipart/form-data`
155+
156+
`FileList` 对象可以被直接传递:
157+
158+
```js
159+
await axios.postForm('https://httpbin.org/post', document.querySelector('#fileInput').files)
160+
```
161+
162+
所有文件将使用相同的字段名`files[]`发送。

posts/zh/notes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: '注意事项'
33
description: '一些注意事项'
4-
prev_title: 'URL-Encoding Bodies'
5-
prev_link: '/zh/docs/urlencoded'
4+
prev_title: 'Multipart 实体请求'
5+
prev_link: '/zh/docs/multipart'
66
---
77

88
## 语义化

posts/zh/post_example.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,54 @@ function getUserPermissions() {
3333
return axios.get('/user/12345/permissions');
3434
}
3535

36+
const [acct, perm] = await Promise.all([getUserAccount(), getUserPermissions()]);
37+
38+
// OR
39+
3640
Promise.all([getUserAccount(), getUserPermissions()])
37-
.then(function (results) {
38-
const acct = results[0];
39-
const perm = results[1];
41+
.then(function ([acct, perm]) {
42+
// ...
4043
});
41-
```
44+
```
45+
46+
将 HTML Form 转换成 JSON 进行请求
47+
48+
```js
49+
const {data} = await axios.post('/user', document.querySelector('#my-form'), {
50+
headers: {
51+
'Content-Type': 'application/json'
52+
}
53+
})
54+
```
55+
56+
### Forms
57+
58+
- Multipart (`multipart/form-data`)
59+
60+
```js
61+
const {data} = await axios.post('https://httpbin.org/post', {
62+
firstName: 'Fred',
63+
lastName: 'Flintstone',
64+
orders: [1, 2, 3],
65+
photo: document.querySelector('#fileInput').files
66+
}, {
67+
headers: {
68+
'Content-Type': 'multipart/form-data'
69+
}
70+
}
71+
)
72+
```
73+
74+
- URL encoded form (`application/x-www-form-urlencoded`)
75+
76+
```js
77+
const {data} = await axios.post('https://httpbin.org/post', {
78+
firstName: 'Fred',
79+
lastName: 'Flintstone',
80+
orders: [1, 2, 3]
81+
}, {
82+
headers: {
83+
'Content-Type': 'application/x-www-form-urlencoded'
84+
}
85+
})
86+
```

posts/zh/urlencoded.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: '请求体编码'
33
prev_title: '取消请求'
44
prev_link: '/zh/docs/cancellation'
5-
next_title: '注意事项'
6-
next_link: '/zh/docs/notes'
5+
next_title: 'Multipart 实体请求'
6+
next_link: '/zh/docs/multipart'
77
---
88

99
默认情况下,axios将 JavaScript 对象序列化为 `JSON` 。 要以`application/x-www-form-urlencoded`格式发送数据,您可以使用以下选项之一。
@@ -91,3 +91,55 @@ axios.interceptors.request.use(config => {
9191
return config;
9292
});
9393
```
94+
95+
### 🆕 自动序列化
96+
97+
当请求头中的 `content-type``application/x-www-form-urlencoded` 时,Axios 将自动地将普通对象序列化成 urlencoded 的格式。
98+
99+
在浏览器和 `node.js` 环境中都适用:
100+
101+
```js
102+
const data = {
103+
x: 1,
104+
arr: [1, 2, 3],
105+
arr2: [1, [2], 3],
106+
users: [{name: 'Peter', surname: 'Griffin'}, {name: 'Thomas', surname: 'Anderson'}],
107+
};
108+
109+
await axios.post('https://postman-echo.com/post', data,
110+
{headers: {'content-type': 'application/x-www-form-urlencoded'}}
111+
);
112+
```
113+
114+
服务器接收到的数据就像是这样:
115+
116+
```js
117+
{
118+
x: '1',
119+
'arr[]': [ '1', '2', '3' ],
120+
'arr2[0]': '1',
121+
'arr2[1][0]': '2',
122+
'arr2[2]': '3',
123+
'arr3[]': [ '1', '2', '3' ],
124+
'users[0][name]': 'Peter',
125+
'users[0][surname]': 'griffin',
126+
'users[1][name]': 'Thomas',
127+
'users[1][surname]': 'Anderson'
128+
}
129+
````
130+
131+
如果您的服务器框架的请求体解析器(例如`express.js``body-parser`)支持嵌套对象解码,则其接收到的数据将与您提交的数据一样。
132+
133+
以下是一个`express.js`的服务器示例,它将会把接收到的数据作为响应返回:
134+
135+
```js
136+
var app = express();
137+
138+
app.use(bodyParser.urlencoded({ extended: true })); // support url-encoded bodies
139+
140+
app.post('/', function (req, res, next) {
141+
res.send(JSON.stringify(req.body));
142+
});
143+
144+
server = app.listen(3000);
145+
```

zh.lang.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ module.exports = {
8383
{
8484
type: "link",
8585
href: "/docs/urlencoded",
86-
text: "请求体编码",
86+
text: "🆕 请求体编码",
87+
},
88+
{
89+
type: "link",
90+
href: "/docs/multipart",
91+
text: "🆕 Multipart 实体请求",
8792
},
8893
{
8994
type: "heading",

0 commit comments

Comments
 (0)