直接上代码:
function login(){
var map={}; //定义map
map["userName"]=$('#userNameInput').val();
map["passWord"]=$('#passWordInput').val();
$.ajax({
async:true,
url:'../Login.do',
type:'POST',
data: JSON.stringify(map), //map转换为json字符串
dataType:'json',
contentType : 'application/json;charset=utf-8',
success: function(data) { //data接收返回数据
console.log(data);
});
}
后端接收:
@RequestMapping(value="/Login",method=RequestMethod.POST)
@ResponseBody
public String Login(@RequestBody Map<String,String> map,HttpServletRequest request,HttpServletResponse response) {
String info="falied";
UserInfo userInfo= userManagerService.loginCheck(map.get("userName"), map.get("passWord"));//登录验证 不存在返回null
//直接用map.get("key")来获取value值
//登陆成功
if(userInfo!=null) {
info="success";
request.getSession().setAttribute("currentUser", userInfo);
userManagerService.updataendtime((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()), map.get("userName"));
}
return info;
}