关于axios
- 支持多个并发请求,当全部返回数据时,执行.then操作
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// 两个请求现在都执行完成
}))
- 添加responseType属性
可以是 ‘arraybuffer’, ‘blob’, ‘document’, ‘json’, ‘text’, ‘stream’
axios({
method:'get',
url:'http://bit.ly/2mTM3nY',
responseType:'stream'
})
.then(function(response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
})
- 添加headers属性
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
// headers: {'X-Requested-With': 'XMLHttpRequest'},
})
- 跨域时是否使用凭证
// withCredentials 表示跨域请求时是否需要使用凭证
withCredentials: false, // default
属性介绍
maxContentLength
定义允许的响应内容的最大尺寸
maxContentLength: 2000,
responseEncoding 返回的数据编码格式
全局的默认配置
axios.defaults.baseURL = ‘https://api.example.com’;
axios.defaults.headers.common[‘Authorization’] = AUTH_TOKEN;
axios.defaults.headers.post[‘Content-Type’] = ‘application/x-www-form-urlencoded’;
使用拦截器 // 省略…