【问题标题】:How can I use conditionally make an API call using AXIOS? [closed]如何使用 AXIOS 有条件地进行 API 调用? [关闭]
【发布时间】:2021-11-10 19:22:02
【问题描述】:

我正在使用带有 REACTjs 的 AXIOS 我想要一个功能,如果我的第一个 API(GET) 调用返回 Not Found 状态码然后我想有效地发送另一个呼叫。 虽然我通过.catch 方法以Promise<pending> 的形式获取了一些数据。请帮助!!!

【问题讨论】:

    标签: reactjs api axios react-hooks


    【解决方案1】:

    为什么不在 catch 块中添加条件来检查状态?

    例如:

    axios.get('/first-call')
      .then(resp => { 
         //handle successful response
      })
      .catch(error => {
        if (error.response && error.response.status === 404) {
         // make second API call
        }
      });
    

    【讨论】:

    • 非常感谢!!!!
    猜你喜欢
    • 2021-10-24
    • 2018-10-24
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2017-12-22
    • 2017-08-31
    相关资源
    最近更新 更多