我正在开发一个项目,将一些 Ajax 添加到 Spring-MVC 项目中。我删除了标签
<form:errors path="fieldName"/>
这将在 JSP 中插入错误,现在我正在尝试制作一些 Ajax 代码来在页面上显示错误。谁能让我更新以下代码以显示错误消息而不是错误?
success: function(response)
{
// we have the response
$('#error').hide('fast');
if(response.status == "SUCCESS")
{
$('#error').hide('fast');
alert("Thanks for submitting. We will get back to you as soon as possible.\n\n" + response.result);
$('#error').hide('slow');
}else
{
errorInfo = "";
for( i = 0 ; i < response.result.length ; i++){
errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
}
$('#error').html("Please correct following errors: " + errorInfo);
$('#info').hide('slow');
$('#error').show('slow');
$('#input').show('slow');
}
},
error: function(e){
alert('Error: ' + e);
}
});
上面的代码用于显示错误但不显示错误消息
下面是我的java代码:
@RequestMapping(value = "/ajax.html", method = RequestMethod.POST)
public @ResponseBody
JsonResponse addMessage(@Valid
@ModelAttribute(value = "memberrequest") MemberRequest memberrequest,
BindingResult result) {
JsonResponse res = new JsonResponse();
if (!result.hasErrors()) {
res.setStatus("SUCCESS");
// Setting data over to RT
String Ticket = zzz.sentWebRequest(memberrequest);
/*
* Setting out the ticket number to be displayed to user
*/
Map<String, Object> model = new HashMap<String, Object>();
Ticket t = new Ticket();
t.setTicketDetails(Ticket);
model.put("ticket", t);
res.setResult(Ticket);
} else {
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
}
return res;
}
}
JSON类:
公共类 JsonResponse { 私有字符串状态 = null; 私有对象结果 = null;
public String getStatus() { 返回状态;}
public void setStatus(String status) { this.status = status; }
public Object getResult() { 返回结果;}
public void setResult(Object result) { this.result = result; }
}