Moving Spring3 MVC Errors to Ajax?
I am working on a project adding some Ajax to a Spring-MVC project. I removed the tags
<form:errors path="fieldName"/>
That would insert the error the JSP, now I am trying to make some Ajax code to display the errors on the page. can anyone should me how to updated the following code to display the error messages and NOT the errors?
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.nn" + 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);
}
});
The above code works to display the errors but not the error messages
below is my java code:
@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 Class:
public class JsonResponse { private String status = null; private Object result = null;
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public Object getResult() { return result; }
public void setResult(Object result) { this.result = result; }
}
尝试response.result[i].defaultMessage
而不是response.result[i].code
。
response.result
There is no result
attribute in response
object according to XMLHttpRequest 2 by W3C
If you pass some text value in response form server end. you can get it response.responseText
as a whole text.
上一篇: 使用curl将文件上传POST数据