我有这个代码块来通过 jquery .post() 方法发布 HTTP 请求。
$.post("/product/update", formPostData)
.done(function (data) {
// success
alert(data.product_id + ' was updated!');
})
.fail(function (data) {
// fail, but which request?
});
当它成功时,很容易知道我们正在处理哪个请求,因为服务器返回的 json 具有我需要的 'product_id'
。
但如果由于服务器没有响应的错误而失败,例如连接问题,我如何知道哪个请求失败了?
data
对象没有任何线索,因为它只包含服务器响应。
如何将值传递给 .fail()
处理程序,以便确定哪个请求失败了?
最佳答案
this
对象在这里很有用。您应该能够解析 this.data
并从那里获取您的帖子信息:
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(this.data);
});
关于javascript - 如何判断哪个ajax请求失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36316051/