React axios 触发一堆 get 请求
React axios triggers bunch get requests
我有一个服务器和一个路由('/is-working'),我在其中简单地呈现是或否,在 React 中我每秒用 Axios 做一个获取请求,使用间隔来查看它是否工作.
代码如下:
useEffect(() => {
const interval = setInterval(async () => {
await axios.get('http://192.168.1.57/is-working')
.then(res => {
if (res?.status === 200) {
// Do somthing
}
})
.catch(err => {
return Promise.reject(err);
})
}, 1000);
return () => clearInterval(interval);
}, []);
我想计算收到 200 响应的次数,当我再次停止托管 http://192.168.1.57/is-working and it returns an network error, until now everything is fine, but when I start hosting http://192.168.1.57/is-working 时,它会一起执行大量获取请求。好像是在请求所有返回网络错误的。
提前感谢您的帮助。
那一定是因为你没有为axios客户端定义超时。因此,它仍在尝试请求您的端点,一旦返回,它会立即执行所有请求
看这里:https://github.com/axios/axios#axioscreateconfig
我有一个服务器和一个路由('/is-working'),我在其中简单地呈现是或否,在 React 中我每秒用 Axios 做一个获取请求,使用间隔来查看它是否工作.
代码如下:
useEffect(() => {
const interval = setInterval(async () => {
await axios.get('http://192.168.1.57/is-working')
.then(res => {
if (res?.status === 200) {
// Do somthing
}
})
.catch(err => {
return Promise.reject(err);
})
}, 1000);
return () => clearInterval(interval);
}, []);
我想计算收到 200 响应的次数,当我再次停止托管 http://192.168.1.57/is-working and it returns an network error, until now everything is fine, but when I start hosting http://192.168.1.57/is-working 时,它会一起执行大量获取请求。好像是在请求所有返回网络错误的。
提前感谢您的帮助。
那一定是因为你没有为axios客户端定义超时。因此,它仍在尝试请求您的端点,一旦返回,它会立即执行所有请求 看这里:https://github.com/axios/axios#axioscreateconfig