I use Spring MVC on the server side, but on one of the pages I decided to create an AJAX check using jQuery, and not the default Spring. Everything works fine, except when I have to do a remote check to see if the title already exists. For javascript, I have the following:
var validator = $("form").validate({
rules: {
title: {
minlength: 6,
required: true,
remote: {
url: location.href.substring(0,location.href.lastIndexOf('/'))+"/checkLocalArticleTitle.do",
type: "GET"
}
},
html: {
minlength: 50,
required: true
}
},
messages: {
title: {
required: "A title is required.",
remote: "This title already exists."
}
}
});
Then I use Spring -Json to do this check and give an answer:
@RequestMapping("/checkLocalArticleTitle.do")
public ModelAndView checkLocalArticleTitle(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map model = new HashMap();
String result = "FALSE";
try{
String title = request.getParameter("title");
if(!EJBRequester.articleExists(title)){
result = "TRUE";
}
}catch(Exception e){
System.err.println("Exception: " + e.getMessage());
}
model.put("result",result);
return new ModelAndView("jsonView", model);
}
However, this does not work, and the "title" field is never checked. I think the reason is because I am returning the answer this way:
{result:"TRUE"}
when in fact the answer should be:
{"TRUE"}
I don't know how to return a single response like this using a ModelAndView answer.
Another thing that doesn't work is the configured message for checking "remote":
messages: {
title: {
required: "A title is required.",
remote: "This title already exists."
}
},
, .
, , Spring jQuery . , jQuery valdations Spring -Json.
.