您的位置:首页 > 其它

利用ajax异步校验用户名

2018-03-14 14:30 337 查看
1.在需要验证的用户名框里面增加onblur焦点移除事件<td class="datalist_right" width="140"><span class='red_text'>*</span>登录账号:</td>
<td class="datalist_left" width="150px"><s:textfield name="person.loginNo" id="loginNo" cssStyle="width:99%" onblur="checkName();"/></td>2.定义ajax异步传值给后台(这里以弹框为例) function checkName(){
$.ajax({
type: "post",
url:"/system/person/checkName.action",
data: {username: $("#loginNo").val()},
success: function(msg){ //msg为后台处理结果后返回的信息
if("success"==msg){
alert("用户名已存在");
return true;
}
},
error: function(){
alert("调用异常");
}
});
}3.前台值已传入后台方法(/system/person/checkName.action)public void checkName() throws Exception{
String username = getRequest().getParameter("username");
if(perSer.checkName(username)){
toClient("success");
}
}4.toClient为已封装方法,后台主要查询数据库是否存在相同账户 protected void toClient(String content) throws Exception
{
content = content ==null ? "":content;
// 使用当前应用的编码
String encoding = "utf-8";
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding(encoding);
PrintWriter pw = response.getWriter();
pw.print(content);
pw.flush();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: