SpringMVC实现AJax异步刷新

来源:互联网 发布:中国联通软件研究院吧 编辑:程序博客网 时间:2024/05/29 21:16

Js脚本代码:

$(function() {
$("#userId").blur(function() {
var _userCode = $(this).val();
$.ajax({
type:"GET",// 请求类型
url:"../user/userCodeIsExix",// 请求的url
data:{"userCode":_userCode},// 请求参数
dataType:"json",// 返回的数据类型
success:function(data) {
if (data.userCode == "exist") {
$("#userId").next().css({"color":"red"}).html("用户名已存在");
} else {
$("#userId").next().css({"color":"green"}).html("用户可以使用");
}
}
});
});
});


Controller代码:

//对用户帐号进行校验
@RequestMapping(value="/userCodeIsExix")
@ResponseBody
public Object userCodeIsExit(String userCode){
log.info("进入userCodeIsExit=======================================");
HashMap<String, String> map = new HashMap<String, String>();
if(StringUtils.isNullOrEmpty(userCode)){//判断userCode是否为空
map.put("userCode", "exist");
}else {
User user=userService.userCodeIsExit(userCode);
if(user!=null){
map.put("userCode", "exist");
}else {
map.put("userCode", "noexist");
}
}
return JSONArray.toJSONString(map);
}

原创粉丝点击