首页 > 代码库 > ajax实例2

ajax实例2

前台:

function save() {
            var username = document.getElementById("username").value;
            var id = document.getElementById("id").value;
            var telephone = document.getElementById("telephone").value;
            var address = document.getElementById("address").value;
            if(!(/^1[34578]\d{9}$/.test(telephone))){ 
                alert("手机号码有误,请重填");  
                return false; 
            } 
            $.ajax({
            url:"UserAction_modify.do",
            data:{
                    "username":username,
                    "telephone":telephone,
                    "id":id,
                    "address":address
                 },
            type:"post",
            dataType:"json",
            success:function(data){
                alert(data.message);
            },
            error:function(data){
                alert(data.message);
            }
        });
    }

后台:

public void modifypsw() throws Exception {
        JSONObject json = new JSONObject();
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
        String newpsw = request.getParameter("newpsw");
        User user = userService.getEntity(id);
        if(!password.equals(user.getPassword())){
            json.put("message", "密码输入错误");
        }else{
            user.setPassword(newpsw);
            userService.saveOrUpdateEntity(user);
            json.put("message", "修改成功");
        }
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

ajax实例2