处理在 jQuery 的 ajax 请求中返回整个 HTML 文档的服务器端 HTTP 4nn/5nn 错误 [英] Handling of server-side HTTP 4nn/5nn errors returning a whole HTML document in jQuery's ajax requests

查看:14
本文介绍了处理在 jQuery 的 ajax 请求中返回整个 HTML 文档的服务器端 HTTP 4nn/5nn 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重点:您将如何处理 jQuery 的 ajax 请求中的服务器端 HTTP 4nn/5nn 错误?此案例涉及服务器端的 JSP/Servlet Web 应用程序.这里我不是在谈论诸如 NullPointerException 之类的琐碎的运行时异常.假设它们都得到了完美的处理.这种 HTTP 4nn/5nn 错误的一个很好的例子是 401 未授权(用户权限不足)和 500 内部服务器错误(数据库关闭、I/O 错误、Errors 等).假设它们不能(或不应该)在编码级别被捕获.

To the point: how would you handle a server-side HTTP 4nn/5nn errors in jQuery's ajax requests? This case concerns a JSP/Servlet webapplication at the server side. Here I am not talking about trivial runtime exceptions such as NullPointerException and so on. Assume that they're all handled perfectly. A good example of such a HTTP 4nn/5nn error is 401 unauthorized (insufficient user rights) and 500 internal server error (database down, I/O error, Errors, etc). Assume that they cannot (or should not) be caught at coding level.

现在我刚刚在 web.xml 中为这些类型的错误声明了一个 .它基本上将请求转发到预定义的 JSP/HTML 错误页面,其中通知最终用户发生了严重错误,并且用户可以联系 xx@xx.xx 以获得进一步的帮助.同一页面还显示有关错误/异常的全局详细信息.

Right now I have just declared an <error-page> in web.xml for those kind of errors. It basically forwards the request to a predefinied JSP/HTML error page wherein the enduser is informed that a serious error has occurred and that the user can contact xx@xx.xx for further assistance. The same page also displays the global details about the error/exception.

它在常规 HTTP 请求中完美运行,但是您将如何使用 jQuery 在 XMLHtttp 请求中处理它?什么是最好的用户体验?对我来说,它只会显示整个错误页面,就好像它是一个普通的 HTTP 请求一样.我已经解决了如下:

It works perfectly in regular HTTP requests, but how would you handle it in XMLHtttp requests using jQuery? What's the best for the user experience? To me, it would be just displaying the entire error page as if it is a normal HTTP request. I've solved it as follows:

function init() {
    $.ajaxSetup({
        error: handleXhrError
    });
}

function handleXhrError(xhr) {
    document.open();
    document.write(xhr.responseText);
    document.close();
}

虽然它工作得很好,但对我来说感觉就像一个黑客.用 HTTP 错误页面的内容替换整个文档.但这也是你会遵循的方式吗?如果没有,您能否详细说明为什么不这样做以及您更喜欢哪种方式?我看到的唯一替代方法是使用 JS 显示一些警报/消息框来通知用户无法解决的错误,但用户可以关闭它并继续页面,而这应该是不可能的.

Although it works perfectly, it feels to me like a hack. Replacing the entire document with the contents of the HTTP error page. But is that also the way you would follow? If not, can you maybe elaborate why not and what way you'd prefer? The only alternative I see is using JS to display some alert/message box to inform the user about the unresolveable error, but the user could dismiss it and continue with the page while that should not be possible.

推荐答案

毕竟,处理异步请求错误的方式和处理同步请求一样,对用户体验来说是最好的.只是,

After all, dealing with errors on asynchronous requests the same way as with synchronous requests is the best for user experience. Only, the

function handleXhrError(xhr) {
    document.open();
    document.write(xhr.responseText);
    document.close();
}

将在 IE6/7/8 中失败,当您有 <script> 元素需要加载和初始化时.IE6/7/8 即不会那样做.为了让 IE 做到这一点,你基本上需要保留当前的 ​​<head> 元素,并且只用 $("head").html(newHead). 元素也应该这样做.

will fail in IE6/7/8 when you've <script> elements which needs to be loaded and initialized as well. IE6/7/8 namely won't do that. In order to get IE to do that, you basically need to keep the current <head> element and only replace its children by $("head").html(newHead). The same should be done for the <body> element.

function handleXhrError(xhr) {
    var newDocument = xhr.responseText;
    $("head").html(newDocument.substring(newDocument.indexOf("<head>") + 6, newDocument.indexOf("</head>")));
    $("body").html(newDocument.substring(newDocument.indexOf("<body>") + 6, newDocument.indexOf("</body>")));
}

令人讨厌,但是当响应文本表示具有头部和主体的整个 HTML 文档时,不可能执行 $(xhr.responseText).当您在错误页面模板的 <body> 上有一些属性(例如 ID、类等)时,不要忘记相应地更改子字符串索引.

Nasty, but it's not possible to do a $(xhr.responseText) when the response text represents a whole HTML document with a head and body. Don't forget to alter the substring index accordingly when you've some attributes on <body> of the error page template (e.g. ID, class, etc).

这篇关于处理在 jQuery 的 ajax 请求中返回整个 HTML 文档的服务器端 HTTP 4nn/5nn 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆