我有一些jQuery代码,它们对Java后端进行REST调用.处理后端函数可能会遇到异常.将此信息备份到Javascript的最佳方法是什么?在测试中,我在Java中捕获了异常,并将HTTP状态代码设置为500.这导致$ .ajax错误处理程序被调用,如预期的那样.错误处理程序的args实际上并不包含任何有用的信息.理想情况下,我想以某种方式将Exception.getMessage()字符串传播回错误处理程序,但不知道如何.
function handleClick() {
var url = '/backend/test.json';
$.ajax({
type: "POST",
url: url,
cache: false,
dataType: "json",
success: function(data){
alert("it worked");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR);
alert(textStatus); // this comes back as "error"
alert(errorThrown); // this comes back as "undefined"
}
});
}
小智 10
我能够以这种方式将自定义错误消息(java String)发送回基于jQuery的客户端.我认为我的自定义消息可以替换为您想要/需要的异常信息
public static void handleRuntimeException(Exception ex, HttpServletResponse
response,String message) {
logger.error(ex);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(message);
response.flushBuffer();
}
Run Code Online (Sandbox Code Playgroud)
displayError:function(jqXHR, textStatus, errorThrown){
if(jqXHR.responseText !== ''){
alert(textStatus+": "+jqXHR.responseText);
}else{
alert(textStatus+": "+errorThrown);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助/
| 归档时间: |
|
| 查看次数: |
17304 次 |
| 最近记录: |