1

When I get the status code 400 I handle the XMLHttpRequest object in this way (1) and it work. Anyway I suppose this code is quite specific to my use case.

I would like to be more generic like:
1) How should I handle the server error when I get the status code 500?


(1)

_.each((JSON.parse(xhr.responseText)).children, function (xhrObject, name) {
    element.find('[name="' + name + '"]')
        .parent().append($('<div>')
        .attr('class', 'is-input-invalid')
        .text(xhrObject.errors));
});
4

1 回答 1

2

这取决于应用程序。为了响应 500,您可能需要做三件事:

  1. 报告发生错误。
  2. 忽略它。
  3. 试试别的。

当操作对用户不重要时,第二个可能会很好(也许他们甚至不知道你在做一些后台更新检查),否则绝对要避免(唯一比报告错误更糟糕的事情对您来说,没有向您报告错误)。第三个是非常专业的(尝试的“其他东西”可能很明显,也可能是不可能的,这取决于您首先尝试做什么)。

要从 500 中获取任何有用的信息,您将需要代码的服务器端向您发送一些有用的信息作为此类消息的主体。究竟什么是“有用的”取决于你在做什么(例如纯文本、json、xml 是你的其余代码最有可能已经设置好处理的东西)。

于 2012-08-27T10:14:40.680 回答