在我的脚本中,我发送数据以检查数据库中是否存在用户名,但无论如何都会创建用户。我检查了控制器工作正常。我是否以正确的方式处理结果?
我的剧本
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 600,
width: 350,
modal: true,
buttons: {
"Create an account": function() {
var bValid = true;
allFields.removeClass( "ui-state-error" );
var values = $("form").serialize();
$.ajax({
type: "POST",
url: "/checkLogin",
data: values,
success: function(result) {
bValid = result == 'true';}})
if ( bValid ) {
var values = $("form").serialize();
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/addUser",
data: values
})
$( "#users tbody" ).append( "<tr>" +
"<td>" + login.val() + "</td>" +
"<td>" + fname.val() + "</td>" +
"<td>" + lname.val() + "</td>" +
"<td>" + bday.val() + "</td>" +
"<td>" + role.val() + "</td>" +
"<td>"+ "<a href=\"/edit/" + login.val() + "\">Edit </a>"
+ "<a href=\"/delete/" + login.val() + ".json\">Delete</a></td>" +
"</tr>" );
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
$( "#create-user" )
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
});
这是我的控制器
@RequestMapping(value="/checkLogin", method=RequestMethod.POST)
@ResponseBody
public boolean check(@RequestParam("login") String login) {
return (userService.findByLogin(login) == null);
}
这是我的html
<div id="dialog-form" title="Create new user">
<form:form >
<fieldset>
<label for="login">Login</label>
<input type="text" name="login" id="login" class="text ui-widget-content ui-corner-all" />
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form:form>
</div>
<button id="create-user">Add user</button>