axios
axios中文说明
- 使用npm:
$ npm install axios
- 使用CDN:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
GET
axios.get('/user?ID=12345')
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
axios('/user?ID=12345')
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
POST
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
axios({
method: 'post',
url: '/user',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
其它请求方法
- axios.request(config)
- axios.get(url[, config])
- axios.post(url[, data[, config]])
- axios.delete(url[, config])
- axios.head(url[, config])
- axios.put(url[, data[, config]])
- axios.patch(url[, data[, config]])
并发请求
axios.all([axios.get('/user/12345'), axios.get('/user/12345/permissions')])
.then(
axios.spread(
function(response1, response2){
}
)
);
请求配置结构
{
url: '/user',
method: 'get',
baseURL: 'https://some-domain.com/api/',
transformRequest: [function(data){
return data;
}],
transformResponse: [function(data){
return data;
}],
headers: {'X-Requested-With': 'XMLHttpRequest'},
params: {
ID: 12345
},
paramsSerializer: function(params){
return Qs.stringify(params, {arrayFormat: 'brackets'})
},
data: {
firstName: 'Fred'
},
timeout: 1000,
withCredentials: false,
adapter: function(config){
},
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
responseType: 'json',
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
onUploadProgress: function(progressEvent){
},
onDownloadProgress: function(progressEvent){
},
maxContentLength: 2000,
validateStatus: function (status) {
return status >= 200 && status < 300;
},
maxRedirects: 5,
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
proxy: {
host: '127.0.0.1',
port: 9000,
auth: {
username: 'username',
password: 'password'
}
},
cancelToken: new CancelToken(function(cancel){
})
}
响应结构
{
data: {},
status: 200,
statusText: 'OK',
headers: {},
config: {},
}
设置全局默认值
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';
拦截器
var myRequestInterceptor = axios.interceptors.request.use(
function(config){
return config;
},
function(error){
return Promise.reject(error);
}
);
axios.interceptors.request.eject(myRequestInterceptor);
var myResponseInterceptor = axios.interceptors.response.use(
function(response){
return response;
},
function(error){
return Promise.reject(error);
}
);
axios.interceptors.response.eject(myResponseInterceptor);
错误处理
axios.get('/user/12345')
.catch(function(error){
if(error.response){
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}else{
console.log('Error', error.message);
}
console.log(error.config);
});
axios.get('/user/12345', {
validateStatus: function(status){
return status < 500;
}
});