前端
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
<head>
<meta charset="UTF-8">
<title>Login</title>
<script type="text/javascript">
function login() {
$.ajax({
type: "POST",
contentType: "application/json;charset=UTF-8",
dataType: "json",//预期服务器返回的数据类型
url: "login",
data: JSON.stringify({
'loginId': $('input[name=loginId]').val(),
'password': $('input[name=password]').val()
}),
success: function (data) {
if (data.success) {
window.location.href = 'index.html'
}
},
error: function (data) {
console.log(data)
alert("异常");
}
});
}
</script>
</head>
<body>
<form id ="form1" action="" method="post" onsubmit="return false">
<p>账号:<input type="text" name="loginId" value="admin"/></p>
<p>密码:<input type="text" name="password" value="123456"/></p>
<p><input type="submit" onclick="login();" value="登录"></p>
</form>
</body>
</html>
后端
@ApiOperation(value = "登录", notes = "登录")
@PostMapping("/login")
@ResponseBody
public BaseResponse login(@Valid @RequestBody LoginRequest request){
SysUser sysUser=loginService.login(request.getLoginId(),request.getPassword());
BaseResponse<SysUser> response=new BaseResponse<>();
response.setMessage(sysUser);
return response;
}
@Getter
@Setter
public class LoginRequest {
@NotEmpty(message = "登录名不能为空")
private String loginId;
@NotEmpty(message = "密码不能为空")
private String password;
}