pull/1/head
954270063@qq.com 2020-04-21 12:03:13 +08:00
parent 0683ea26b1
commit 1ff3d8fe98
2 changed files with 41 additions and 40 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
/node_modules
/package
/package.zip
/types
/coverage
/yarn-error.log

View File

@ -181,6 +181,46 @@ axios.post('/test', { test: 1 }, {
|text|响应的数据为文本|是|
|arraybuffer|响应的数据为 ArrayBuffer|是|
#### 默认配置`defaults`
##### 全局默认配置`axios.defaults`
```typescript
axios.defaults.baseURL = 'https://www.xxx.com';
axios.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
```
##### 自定义实例默认配置
可以创建时传入
```typescript
const instance = axios.create({
baseURL: 'https://www.xxx.com',
headers: {
common: {
'Accept': 'application/json, test/plain, */*'
},
post: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}
}
});
```
也可以创建后修改
```typescript
instance.defaults.baseURL = 'https://www.xxx.com';
instance.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
```
##### 配置优先顺序
发送请求时,会使用默认配置`defaults`和自定义配置`config`合并出请求配置`requestConfig`,然后用合并出的请求配置`requestConfig`去发送请求,多数情况下,后者优先级要高于前者,具体合并策略可以参考 [mergeConfig.ts](https://github.com/early-autumn/axios-miniprogram/blob/master/src/core/mergeConfig.ts) 的实现。
#### 自定义合法状态码`config.validateStatus`
可以让请求按照您的要求成功或者失败。
@ -304,46 +344,6 @@ axios.defaults.adapter = function adapter(adapterConfig) {
axios.defaults.adapter = wx.request;
```
#### 默认配置`defaults`
##### 全局默认配置`axios.defaults`
```typescript
axios.defaults.baseURL = 'https://www.xxx.com';
axios.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
```
##### 自定义实例默认配置
可以创建时传入
```typescript
const instance = axios.create({
baseURL: 'https://www.xxx.com',
headers: {
common: {
'Accept': 'application/json, test/plain, */*'
},
post: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
}
}
});
```
也可以创建后修改
```typescript
instance.defaults.baseURL = 'https://www.xxx.com';
instance.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
```
##### 配置优先顺序
发送请求时,会使用默认配置`defaults`和自定义配置`config`合并出请求配置`requestConfig`,然后用合并出的请求配置`requestConfig`去发送请求,多数情况下,后者优先级要高于前者,具体合并策略可以参考 [mergeConfig.ts](https://github.com/early-autumn/axios-miniprogram/blob/master/src/core/mergeConfig.ts) 的实现。
## 响应体`response`
非全平台兼容的属性只会在平台支持的情况下生效。