【问题标题】:redirecting after getting data from api从api获取数据后重定向
【发布时间】:2021-09-29 15:34:47
【问题描述】:

我正在调用api,如果数据来自api,那么这意味着服务器正在运行,我必须重定向到下一页,如果没有来自api的数据,我应该显示消息服务器没有运行。无论数据是什么都无关紧要,数据是否基于我必须重定向,我如何在反应中做到这一点

useEffect(() => { axios .get("我的 api") .then((res) => { 控制台.log(res); 场景(res.data);
}) .catch((错误) => { 控制台日志(错误); }); }, []);

【问题讨论】:

    标签: api redirect routes


    【解决方案1】:

    这是我必须进行的更新,if(res.status===200) 这意味着我们正在从 api 获取数据。

    useEffect(()=>{
         axios.get('your api')
         .then((res)=>{
            if (res.status === 200) {
             history.push('/Scenes')
          
          }else{
           alert("check the server connection")
          }
            
            setsts(res.data)
         
        })
        .catch((err) => {
          console.log(err);
        });
    }, []);
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2018-04-07
      • 2012-08-08
      • 2022-01-24
      • 2018-09-11
      • 2019-09-23
      • 2020-09-04
      • 2019-04-25
      • 2017-06-06
      相关资源
      最近更新 更多