我正在使用 jQuery 的 ajax 方法来提交这样的 ajax 请求:
$.ajax({
type: "PUT",
url: someURL,
contentType: "application/json",
data: JSON.stringify(data),
dataType: "json"
})
如何为 http 状态代码 401 和 404 添加错误处理程序?
最佳答案
来自the jQuery documentation ,您可以在调用 .ajax()
时将特定状态代码响应的处理程序作为选项实现:
$.ajax({
type: "PUT",
url: someURL,
contentType: "application/json",
data: JSON.stringify(data),
dataType: "json",
statusCode: {
404: function() {
// handle the 404 response
},
401: function() {
// handle the 401 response
}
}
})
关于javascript - ajax请求中的404和401错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19320870/