基本使用
btn.onclick = function(){
axios({
method: 请求方式,
url: 请求地址,
data: 请求体,
}).then(res => {
成功的回调操作
})
}
axios.get()就是请求方式为get的方法,语法糖
默认配置
axios.defaults.method = 'GET';
axios.defaults.baseURL = 'http://localhost';
axios.defaults.params = {id=100};
axios.defaults.timeout = 3000;
创建实例对象
const axios1 = axios.create({
});
拦截器
axios.interceptors.request.use(function (config){
console.log('xxx');
return config;
}, function (error){
console.log('aaa');
return Promise.reject(error);
});
axios.interceptors.request.use(function (config){
console.log('yyy');
return config;
}, function (error){
console.log('aaa1');
return Promise.reject(error);
});
axios.interceptors.response.use(function (response){
console.log('iii');
return response;
}, function (error){
console.log('bbb');
return Promise.reject(error);
});
axios.interceptors.response.use(function (response){
console.log('jjj');
return response;
}, function (error){
console.log('bbb1');
return Promise.reject(error);
});
axios({
url:
method:
}).then(res => {
处理
})。catch(res => {
处理
})
取消请求
let cancel = null;
btn[0].onclick = function(){
if(cancel != null){
cancel();
}
axios({
method: 'get',
url: 'path',
cancelToken: new axios.CancelToken(function(c){
cancel = c;
}),
}).then(res => {
console.log(res);
cancel = null;
})
}
btn[1].onclick = function(){
cancel();
}