我不太喜欢 javascript,但我想在我正在从事的这个项目中实现错误对话框。我想发送一个 http header ,错误函数将捕获该 header 并显示正确的消息。我已经用警告框测试了错误函数,当我发送 header 400 时,错误函数被触发。
是否可以在 JQuery 或 javascript 中根据 header 代码(如 400 或 401)显示不同的消息/对话框?基于该代码然后向用户显示正确的错误消息?我需要为每个错误函数显示大约 4 或 5 条消息
error:function(){
// call my dialog here
}
最佳答案
statusCode
选项在 jQuery 1.5+ 中可用 jQuery.ajax
:
A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:
$.ajax({
statusCode: {
404: function() {
alert('page not found');
}
}
});
If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.
关于javascript - jquery错误函数帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5750246/