调用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
相关产品推荐
相关产品推荐

