【发布时间】:2020-04-29 17:14:21
【问题描述】:
我刚刚读完一篇关于Fetch vs Axioshere的文章:
作者指出“Axios 会抛出网络错误,而 fetch 不会。使用 fetch 时,必须始终检查 response.ok 属性”,而 Axios 不需要 if (!response.ok) throw Error(response.statusText)
我不太明白为什么以及如何在 Axios 中处理网络错误以及 Axios 请求不需要 if(!response.statusText !== 'OK') throw new Error(....) 是否正确?
这也是我的 axios 要求,请你至少说它的好标准吗?
const fetchData = async (url) => {
setFetchError(false);
try {
const response = await axios.get('/api/movies');
return response.data;
} catch (err) {
setFetchError(true);
}
};
【问题讨论】:
标签: javascript api axios fetch