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

时间:2021-05-29 21:09:16

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. 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.

servlet中的异常可以有不同的消息,所以我的目的只是将状态代码和消息传递给客户端,以便它可以在一个范围内显示。如果成功,成功块正在接收响应,我看到我在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. I am not favouring this because I feel its tightly coupled and still implementing bad requests or exceptions as successful operation.

    在成功写入out.print和成功阻止Ajax函数中,使用状态代码编写多个检查,并决定是显示成功消息还是错误消息。我并不赞成这一点,因为我觉得它紧密耦合,并且仍然在执行成功操作时执行错误的请求或异常。

  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. So in AJAX, I will be forced to write a parser to get the exception message. This is also NO NO in my mind.

    使用response.sendError(status_code :: int,e.getMessage():: String)尽管这样可以确保调用Ajax中的错误块,但是sendError API会创建一个HTML页面,放入我的消息并发送它,我无法直接访问。所以在AJAX中,我将*编写一个解析器来获取异常消息。这在我看来也不是。

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. And in a span, that success or exception information is displayed, in same form page.

我试图以最好的方式解释,如果需要更多信息,请告诉我。它是一个简单的API,客户端使用表单数据调用servlet,表单数据在服务器中创建/更新,如果某些条件不匹配则失败。并且在一个范围内,在同一表单页面中显示该成功或异常信息。

2 个解决方案

#1


3  

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. I just did:

我试过这个,发现状态400的异常现在进入错误块,我能够在错误块内部用jqXHR.responseText检索消息。我已经做了:

response.setStatus(400);
out.print(e.getMessage());
out.flush();

#2


2  

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");

#1


3  

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. I just did:

我试过这个,发现状态400的异常现在进入错误块,我能够在错误块内部用jqXHR.responseText检索消息。我已经做了:

response.setStatus(400);
out.print(e.getMessage());
out.flush();

#2


2  

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");