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);
}