第一、ajax表单提交
$("#表单").submit(function(){
var option = {
type:"post",
dataType:"text",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success:function(responseText){
if(responseText == "failure"){
alert("更新用户失败");
window.close();
opener.location.reload();
}else{
alert("更新用户成功");
}
},
error:function(){
alert("系统错误");
}
};
$(this).ajaxSubmit(option);
return false;
});
第二、ajax校验
function validateUser(){
$.ajax({
url:"${pageContext.request.contextPath}/LoginAction",//要请求的servlet
data:{method:"queryUserExistAjax",userId:value},//传给服务器的参数
type:"POST",
dataType:"text",
success:function(result){
if(result){//如果后台返回true,用户名存在
return false;
}}
});
return true;
}
后台
public String validRoleName (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String roleName = req.getParameter("1111");
String retString = "failure";
if (isValidRoleName) {
retString = "success";
}
// 3.返回查询结果
resp.setContentType("text/html;charset=GBK");
PrintWriter out = resp.getWriter();
out.write(retString);
return null;
}
阅读(1424) | 评论(0) | 转发(0) |