在Spring-Boot中使用jQuery提交Ajax表单时无法点击控制器方法。
下面是我的html代码:
<div class="main-container">
<div class="col-md-6 offset-3" style="padding-top:20px;">
<form th:object="${user}" method="post" id="registration_form">
<div class="form-group">
<small>Full Name</small>
<input type="text" class="form-control" id="name" name="name" />
</div>
<div class="form-group">
<small>Email</small>
<input type="password" class="form-control" id="email" name="email" />
</div>
<div class="form-group">
<small>Password</small>
<input type="password" class="form-control" id="password" name="password" />
</div>
</form>
<div class="form-group row">
<div class="col-sm-6">
<button id="btn_cancle" class="btn btn-danger" onclick="clear_reg_form()" style="width:100px;">Clear</button>
<button id="btn_save" class="btn btn-success" onclick="submit_reg_form()" style="width:100px;">Save</button>
</div>
</div>
</div>
</div>
下面是我的jQuery代码:
$.ajax({
url: "/register",
type: 'POST',
data: $('#registration_form').serialize(),
success: function(data) {
$("#loader").hide();
var data = jQuery.parseJSON(data);
if(data.is_error == "false"){
$("#msg_div").text(data.message);
} else if(data.is_error == "true"){
$("#msg_div").text(data.message);
}
$("#btn_cancle").removeAttr("disabled");
$("#btn_save").removeAttr("disabled");
},
error: function(jqXHR, textStatus, errorThrown) {
$("#msg_div").html(jqXHR.responseText);
console.log("ERROR : ", jqXHR.responseText);
$("#btn_save_document_link").removeAttr("disabled");
}
});
这是我在控制器中的功能:
@PostMapping("/register")
public HashMap<String, String> saveUser(@Valid User user) {
System.out.println("In registra controller -->>>");
HashMap<String, String> returnMap = new HashMap<>();
if(userService.isEmailExists(user.getEmail())) {
returnMap.put("message", messagesService.emailExists);
} else if(user.getPassword() == "" || user.getPassword().length() < 5) {
returnMap.put("message", messagesService.passwordLength);
} else {
returnMap.put("message", "success");
userService.createUser(user);
}
return returnMap;
}
请帮助我解决此问题。 预先感谢。