代码:
//发送注册异步请求
$.ajax({
type:"post",
url :"/user/register",
data:$("form").serialize(),
dataType:"json",
success:function(data){
alert(data);
if(data == "success"){
alert("注册成功");
window.location.href="/";//你跳转的页面
}else if(data == "fail"){
alert("注册失败:"+data);
}
},
error:function(data){
alert("error:"+data);
if(data == "error")
alert("注册异常"+data);
window.location.href="/";
}
});
@RequestMapping(value = "/user/register",method = RequestMethod.POST)
public ModelMap register(User user){
ModelMap map = new ModelMap();
try {
System.err.print(user.toString());
int result = userService.register(user);
if (result > 0) {
//注册成功
map.put("res","success");
}else {
map.put("res","fail");
}
return map;
}catch (Exception e){
e.printStackTrace();
map.put("data","error");
return map;
}
}
而且window.location.href="/";这行代码也没执行,因为没有跳转
数据库是插入了数据的,也没有抛出异常,为什么会是error呢?
看一下response,http code是否为200,不是的话就会进入error逻辑