const apiUrl = process.env.REACT_APP_API_URL;
interface Config extends RequestInit {
token?: string;
data?: object;
}
export const http = async (
endpoint: string,
{ data, token, headers, ...customConfig }: Config = {}
) => {
const config = {
method: "GET",
headers: {
Authorization: token ? `Bearer ${token}` : "",
"Content-Type": data ? "application/json" : "",
},
...customConfig,
};
if (config.method.toUpperCase() === "GET") {
endpoint += `?${qs.stringify(data)}`;
} else {
config.body = JSON.stringify(data || {});
}
// axios 和 fetch 的表现不一样,axios可以直接在返回状态不为2xx的时候抛出异常
return window
.fetch(`${apiUrl}/${endpoint}`, config)
.then(async (response) => {
if (response.status === 401) {
await auth.logout();
window.location.reload();
return Promise.reject({ message: "请重新登录" });
}
const data = await response.json();
if (response.ok) {
return data;
} else {
return Promise.reject(data);
}
});
};
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
fetch捕捉不了异常
武汉格发信息技术有限公司,格发许可优化管理系统可以帮你评估贵公司软件许可的真实需求,再低成本合规性管理软件许可,帮助贵司提高软件投资回报率,为软件采购、使用提供科学决策依据。支持的软件有: CAD,CAE,PDM,PLM,Catia,Ugnx, AutoCAD, Pro/E, Solidworks ,Hyperworks, Protel,CAXA,OpenWorks LandMark,MATLAB,Enovia,Winchill,TeamCenter,MathCAD,Ansys, Abaqus,ls-dyna, Fluent, MSC,Bentley,License,UG,ug,catia,Dassault Systèmes,AutoDesk,Altair,autocad,PTC,SolidWorks,Ansys,Siemens PLM Software,Paradigm,Mathworks,Borland,AVEVA,ESRI,hP,Solibri,Progman,Leica,Cadence,IBM,SIMULIA,Citrix,Sybase,Schlumberger,MSC Products...