You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用axios.delete接口报错error is not a function问题排查

错误产生原因
  • 核心诱因是Axios返回的Promise实例不存在.error()方法,当前代码在.then()后链式调用了不存在的API,直接触发了"xxx.error is not a function"的类型错误。
  • 该写法属于常见API混淆:.error()是jQuery Ajax提供的错误回调方法,而Axios是基于标准Promise实现的HTTP库,不支持该调用方式。
  • 附带说明:现有代码还存在一处不影响运行的笔误,日志打印内容里的Iteam为拼写错误,正确拼写为Item。
修复方法

将链式调用中错误的.error()替换为Promise标准的.catch()方法即可完成修复,修复后的可运行代码如下:

const deleteApi = (id) => {
  console.log(id);
  axios
    .delete(`http://localhost:5000/delete-list/${id}`)
    .then((res) => {
      console.log("Deleted Item", res);
    })
    .catch((error) => {
      console.log(error);
    });
};

如果后续重构为async/await语法,也可以搭配try/catch代码块实现相同的错误捕获逻辑,两种写法效果完全一致。

内容的提问来源于stack exchange,提问作者Vishnu Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 18:15:44