javascript - jquery post请求捕获失败的请求
问题描述
在前端 JS 中,我正在向后端发送请求:
$.post("/test", data, () => {
console.log("received");
})
.done(() => {
console.log("request finished");
})
.fail(function() {
console.log("error");
})
在后端,我故意导致请求失败:
app.post("/test", (req, res) => {
res.status(400).send("Error message");
})
显然,400 错误没有被.fail
块捕获。我后来尝试使用 try catch 块:
try {
await $.post("/test", data, () => {
console.log("received");
})
}
catch (error) {
console.log(error);
}
尽管如此,错误还是消失了。我现在没有主意了。那么我应该如何捕获 jquery post 请求返回的错误呢?
解决方案
推荐阅读
- ios - 长按 Ionic 应用程序的 IOS 15 应对图像
- anaconda - 如何将 geopandas 更新为最新版本?
- wordpress - 无法在 WP Graph QL 中获取 acf 关系字段值
- typescript - 就地更改其输入类型的函数的类型
- c++ - 两个 uint64_t 数字之间的逻辑与运算的意外结果
- makefile - ./configure 在“检查 make 是否设置 $(MAKE)...”时停止进行
- android - android中CSS % 的等价物是什么?
- pandas - 在 Pandas Dataframe 中出现错误:在 17217 位置没有重复内容
- python-3.x - 一个类可以在 python 中拥有一个内容管理器吗?
- amazon-web-services - AWS CodeDeploy 在 install_dotnetcore 上失败