我在服務器端使用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