我在服务器端使用Spring MVC,但在其中一个页面中,我决定使用jQuery而不是默认的Spring验证创建AJAX验证。 一切工作很好,除非我必须做一个远程验证,以检查数据库中是否存在“标题”。 对于JavaScript的我有以下几点:Spring MVC jQuery远程验证
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."
}
}
});
然后,我用Spring的JSON来使此验证,并给予回应:
@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);
}
但是,这并不工作,并现场“称号“从未被验证。 我想这样做的原因是我的方式返回一个答案:
{result:"TRUE"}
其实时,答案应该是:
{"TRUE"}
我不知道怎么回事像这样使用ModelAndView答案的单个响应。
不工作的另一件事情是“远程”验证定制的消息:
messages: {
title: {
required: "A title is required.",
remote: "This title already exists."
}
},
所需的信息工作,而不是远程消息。 我环顾四周,但我没有看到许多人同时使用Spring和jQuery。至少,不要混用jQuery远程值和Spring-Json。 我很感谢这里的一些帮助。
没有人知道答案吗? – raulsan