1.0.7 • Published 4 years ago
hitcxedu-http v1.0.7
hitcxedu-http
安装
npm i hitcxedu-http -S
初始化 http
import hitcxeduHttp from "hitcxedu-http";
hitcxeduHttp.setOptions({
baseURL: baseURL,
// 请求正确的code
successCode: 200,
// 请求发出前
request(config) {
config.headers['Authorization'] = token
return config;
},
// 请求发送失败
requestError(error) {
console.log(error);
},
// 成功返回
responseSuccess(response) {},
// 常见的公共错误处理
commonErrorHandling(response) {
switch (response.data.code) {
case 101:
console.log(response.data.msg || "登录失效,请重新登录");
break;
default:
console.log(response.data.msg || "网络错误!");
}
},
// 处理返回错误
responseError(error) {}
})
使用说明
// 传递url参数
http.$GET(url, {
query: {
id: 22
}
})
// 需要在url后面拼接ID
http.$GET('url/{id}', {
params: {
id: 2
}
})
// 传递body 参数
http.$POST({
data: {
id: 22
}
})
// 传输 formData 格式
http.$POST({
data: {
id: 22
}
}, { _formData: true })
// 你还可以通过 clearHttpRequestingList 来取消所有处于pending状态的请求
clearHttpRequestingList()