axios使用
axios封装
import axios from 'axios';
import { notification } from 'antd';
const Authorizationaxios = axios.create({
timeout: '360000',//设置连接超时
});
Authorizationaxios.interceptors.response.use(
res => {
return res;
},
error => {
console.info(error);
if (error.code === 'ECONNABORTED') {
notification.error({
description: '您的网络发生异常,无法连接服务器',
message: '网络异常',
});
}
if (error.response.status) {
let code = error.response.status;
console.log(code);
switch (code) {
case 401:
notification.error({
description: '用户没有权限(令牌、用户名、密码错误)',
message: '401 Unauthorized',
});
break;
case 500:
notification.error({
description: '服务器发生错误,请检查服务器。',
message: '500 Internal Server Error',
});
break;
case 504:
notification.error({
description: '网关超时。',
message: '504 Internal Server Error',
});
break;
}
}
return Promise.reject(error);
},
);
export default Authorizationaxios;
axios使用
Authorizationaxios({
//方法
method: 'post',
//url
url: ...省略,
//你想传的数据
data: {},
//返回值类型
//responseType: 'blob',
}).then(res => {
if (res.status === 204) {
//省略
}
});
services里也可修改
import Authorizationaxios from '@/utils/axios';
export async function GetCompany(params) {
return Authorizationaxios({
url: '...',
method: 'GET',
});
}
当然用自带的request也可以
然后开始你的react之坑 旅吧
如果有帮助到你,点个赞吧