0
我正在使用Spring Framework MVC,並試圖讓我的登錄表單工作,但我確實是ajax的初學者。如何在模態登錄表格中防止默認()
這是我的index.jsp的代碼。
<!-- The Modal -->
<div id="modal1" class="modal2">
<span onclick="document.getElementById('modal1').style.display = 'none'" class="close" title="Close Modal">×</span>
<!-- Modal Content -->
<form:form id="login-form" method="POST" commandName="users" class="modal-content2 animate">
<div class="imgcontainer">
<img src="<c:url value=" /public/images/rsz_default-user.png "/>" alt="Avatar2" class="avatar">
</div>
<div class="container">
<p>
<label id="Correo"><b>Correo</b></label>
<input name="correo" id="correo" class="form-control" />
</p>
<p>
<label><b>Password</b></label>
<input type="password" name="password" id="password" class="form-control" />
</p>
<button type="submit"onclick="submitForm();"class="cyan">Enviar</button>
<button type="button" onclick="document.getElementById('modal1').style.display = 'none'" class="red">Cancel</button>
<span class="psw">Olvidaste tu <a href="#">Contraseña?</a></span>
</div>
</form:form>
</div>
這裏是我的LoginController:
@RequestMapping(value = "/index.htm", method = RequestMethod.POST)
public @ResponseBody String login(@ModelAttribute("users") Users u,BindingResult result) {
String returnText;
this.userValidations.validate(u, result);
Users user = checkUser(u.getCorreo(), u.getPassword());
if ((u.getCorreo().equals(user.getCorreo())) && (u.getPassword().equals(user.getPassword()))) {
returnText = "¡[email protected] " + user.getNombre() + "!";
} else {
returnText = "Usuario incorrecto";
}
return returnText;
}
,這是我的AJAX功能提交模式窗體如果登錄全成。
function submitForm(){
$('#login-form').on('submit', function (e) {
e.preventDefault();
var form = $(this);
alert("Estoy aca");
$.ajax('/index.htm', {
type: 'POST',
data: form.serialize(),
success: function (result) {
alert("Estoy aca");
Materialize.toast(result);
}
});
});
}
我在這裏的問題是,當我填寫表格正確工作正常提交,但如果我把錯誤的數據到表單中,我不知道如何捕捉到錯誤進入模式不刷新或重定向頁面。