javascript - jquery错误函数帮助

标签 javascript jquery

我不太喜欢 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/

相关文章:

Javascript Innerhtml - 奇怪的样式

用于单击背景+元素的 Jquery 选择器?

javascript - ajax完成后调用一个函数

javascript - 我收到警报错误的 js fiddle 配置

javascript - Highchart 在日期图表中显示错误的月份

javascript - CoffeeScript:除非如何添加类

javascript - 使用 jquery 从对象数组中获取属性值数组

javascript - 从远程 html 页面加载 div 并将其附加到当前页面

javascript - 如何在忽略空值的同时扩展对象?

javascript - 如何为多个ajax调用设置延迟并为其绑定(bind)停止按钮?