Axios文档
1.并发请求
- 门 & 闩
- axios.all(iterable)
- axios.spread(callback)
axios.all(
[getOne(), getTwo()]
).then(
axios.spread(
function(oneResponse, twoResponse){
}
)
)
2.axios API
axios(
{
method: 'post',
url: '/user/getById',
data: '20134081'
})
axios({
method: 'get',
url: 'http://image',
responseType: 'stream'
}).then( response => {
response.data.pipe(fs.createWriteStream('image.jpg'))
}
)
- 请求方法的别名 axios.get();(柯里化)
- 创建实例 axios.create()
3.请求配置
- url
- method
- baseURL
- transformRequest 发送给服务器前,修改请求数据
- transfromResponse 传递给then和catch之前修改响应
- headers
- params
- paramsSerializer
- data: 是作为请求体被发送的数据
- string,plain object,ArrayBuffer,URLSearchParams
- 浏览器: FormData,File,Blob
- Node: Stream
- timeout 请求超时
- withCredentials: false 跨域凭证
- adapter
- auth: Authorization
- responseType: ‘json’, arraybuffer,blob,document,text,stream
- responseEncoding:‘utf8’
- maxContentLength 响应内容最大尺寸
- validateStatus 决定resolve还是reject
- maxRedirects 最大重定向数
- httpAgent
- proxy
- cancelToken
4.响应结构
- data: {}
- status: 200
- statusText:‘OK’
- headers
- config
- request
5.配置默认值
axios.defaults.baseURL = 'xxx'
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
- 自定义实例默认值
- 配置的优先顺序
- lib/defaults.js
- 实例的defaults
- 请求的config
6.***interceptors
- 在请求或响应被then和catch处理之前拦截它们
axios.interceptors.request.use(
function(config){
return config
},
function(error){
return Promise.reject(error)
}
)
axios.interceptors.request.eject(myTnterceptor)