对于 Spring MVC Controller 中的验证错误,我将自定义 header error 设置为响应 header 。我可以在 Firefox 3.5 中访问该响应 header 。但在 IE 8 中不行。 请告诉我向 Jquery 客户端显示自定义错误消息的正确方法。
var jqxhr=$.post("saveAcc.htm",{ data: data});
jqxhr.success(function() {
alert("Saved");
});
jqxhr.error(function(thrownError){
fnSetError(jqxhr.getResponseHeader('error'));
alert(jqxhr.getAllResponseHeaders()); //returns empty
//alert('responseText '+ thrownError.responseText);
alert(jqxhr.getResponseHeader('error')); //return null
oTable.fnReloadAjax();
});
请您参考如下方法:
如果要调用JS错误处理函数,请在服务器上抛出异常。
或者,您可以将 HTTPStatus 设置为 500 或其他错误状态
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
//response.setStatus(500);
但我认为你的做法是错误的。验证错误不是应用程序错误,您应该在成功消息中处理这些错误。
jqxhr.success(function(data) {
if(data['validationMessages'].length > 0 ) {
//deal with the validation issues
}
});
这可能有帮助:http://outbottle.com/spring-3-web-mvc-exception-handling-incorporating-ajax/
