简体   繁体   English

处理来自Servlet的Jquery AJAX响应中的异常

[英]Handling exception in Jquery AJAX response from Servlet

My servlet code is 我的servlet代码是

try{
    //something
    response.setStatus(201);
    out.print("Data successfully saved");
}
catch(SomeException e){
    response.sendError(400, e.getMessage());
}

My JQuery Ajax function has success and error blocks as follows: 我的JQuery Ajax函数有成功和错误块,如下所示:

$.ajax({
    type:'POST',
    data: {
        //some data to be passed
    },
    dataType: 'text',
    url:'AjaxController',
    success: function(response){
        alert(response);
    },
    error: function(jqXHR, textStatus, message) {
        //error handling
    },
    complete: function(){
        //window.location.reload(true);
    } 
});

The exception in servlet can have different messages, so my intention is just to pass on the status code and the message to client, so that it can be displayed in a span. servlet中的异常可以有不同的消息,所以我的目的只是将状态代码和消息传递给客户端,以便它可以在一个范围内显示。 In case of success, the success block is receiving the response and I saw that anything I write in out.print() is coming inside it. 如果成功,成功块正在接收响应,我看到我在out.print()写的任何内容都在其中。

But in case of error to show up the message, I have two options: 但是如果出现错误显示消息,我有两个选择:

  1. Writing same as success to out.print and in success block in the Ajax function, write multiple checks with status code and decide whether to show a success message or a error message. 在成功写入out.print和成功阻止Ajax函数中,使用状态代码编写多个检查,并决定是显示成功消息还是错误消息。 I am not favouring this because I feel its tightly coupled and still implementing bad requests or exceptions as successful operation. 我并不赞成这一点,因为我感觉它紧密耦合,并且仍然在执行成功操作时执行错误的请求或异常。

  2. Using response.sendError(status_code::int, e.getMessage()::String) Even though this make sure the error block in Ajax is invoked, but the sendError API creates a HTML page, puts in my message and sends it, which I can not directly access. 使用response.sendError(status_code::int, e.getMessage()::String)尽管这样可以确保调用Ajax中的错误块,但sendError API会创建一个HTML页面,然后放入我的消息并发送它,我无法直接访问。 So in AJAX, I will be forced to write a parser to get the exception message. 所以在AJAX中,我将被迫编写一个解析器来获取异常消息。 This is also NO NO in my mind. 这在我看来也不是NO。

How can I achieve this exception handling? 我该如何实现这种异常处理?

I have tried to explain in best possible manner, let me know if more info needed. 我试图以最好的方式解释,如果需要更多信息,请告诉我。 Its a simple API, client makes call to servlet with form data, which is either created/updated in server or is failed if some conditions do not match. 它是一个简单的API,客户端使用表单数据调用servlet,表单数据在服务器中创建/更新,或者在某些条件不匹配时失败。 And in a span, that success or exception information is displayed, in same form page. 并且在一个范围内,在同一表单页面中显示该成功或异常信息。

I have tried this and found that exceptions with status 400 is now coming in error block and i was able to retrieve the message with jqXHR.responseText inside error block. 我试过这个,发现状态400的异常现在进入错误块,我能够在错误块内部用jqXHR.responseText检索消息。 I just did: 我已经做了:

response.setStatus(400);
out.print(e.getMessage());
out.flush();
var response = $.parseHTML(xhr.responseText);
if(xhr.status == 400) {
    alert($(response).filter( 'h1' ).text());
}

You can use the code from above to filter the status code and then print the modified message sent from server. 您可以使用上面的代码过滤状态代码,然后打印从服务器发送的修改后的消息。

JSP may look like this JSP可能看起来像这样

response.sendError(400, "Length too long");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM