ajax实例2
Posted O_wen 快到碗里来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章