为了账号安全,请及时绑定邮箱和手机立即绑定

axios 完成事件

axios 完成事件

芜湖不芜 2023-08-05 19:33:59
我正在使用 Axios 进行 API 服务,只是好奇是否有任何官方方法来处理我们在 Ajax 调用中使用的“完整”事件。所以喜欢axios.get('/v1/api_endpoint?parameters')   .then((res) => { .. })   .catch((err) => { .. })   .complete(() => {})     //  <== is there any way to handle this complete event?
查看完整描述

2 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

这是一个很好的示例,说明如何处理 axioscomplete 事件,无论成功还是失败,该事件都将始终执行。

axios.get('/v1/api_endpoint?with_parameters')
  .then((res) => { // handle success })
  .catch((err) => { // handle error })
  .then(() => { // always executed })        <-- this is the one


查看完整回答
反对 回复 2023-08-05
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

如果您需要检查API调用是否成功,可以使用以下代码:


const response = await axios.post(

      "http://localhost:8000/xyz",

      { token, user }

    );

    const status = response.status

    if (status == 200) {

      console.log('Success')

      toast("Successful Transaction", { type: "success" });

    } else {

      console.log('Falure')

      toast("Falure", { type: "error" });

    }

您还可以用来finally检查事件是否完成。

查看完整回答
反对 回复 2023-08-05
  • 2 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信