ajax 几种提交方式

方式一:



$.ajax({ type:
'POST', url: "/user/editPwd.htm", data: {"oldPassword":oldPassword,"newPassword":newPassword}, success: function(data){ if ('success' == data){ alert("修改成功"); window.location.href="/user/index.htm"; } else { alert('修改失败:旧密码错误'); } } })


java代码:

@RequestMapping(value = "/editPwd.htm", method = RequestMethod.POST)
    @ResponseBody
    public String editPwd(HttpServletRequest request, String oldPassword, String newPassword) {
        UserContext userContext = (UserContext) request.getSession().getAttribute("userContext");

        UserInfo old = userFacade.findUserById(userContext.getUserId());

        if (StringUtils.md5(oldPassword).equals(old.getPassword())) {
            old.setPassword(StringUtils.md5(newPassword));
            old.setUpdateTime(System.currentTimeMillis());

            userFacade.editUser(old);
        } else {
            return FAIL;
        }

        return SUCCESS;
    }

  

方式二:

提交对象:

var obj = {};
$.ajax({ url:
"/user/save.htm", type: "post", dataType: "json", contentType: "application/json", data: JSON.stringify(obj), success: function(data){ if ('success' == data) window.location.href="/user/login.htm"; else if ('exist' == data) { alert(phone + '已注册'); } else { alert('注册失败'); } } })

 

java代码:

@RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    @ResponseBody
    public String add(HttpServletRequest request, @RequestBody UserInfo user) {

        // 相同手机号不能再注册
        UserInfo checkUser = userFacade.findUserInfoByPhone(user.getPhone());
        if (checkUser == null) {
            user.setUserName("u_" + DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMddHHmmss") + user.getPhone().substring(7));
            userFacade.saveUser(user);
        } else {
            return "exist";
        }

        return SUCCESS;
    }

以上暂时想到了两种post。

@RequestMapping(value = "/editPwd.htm", method = RequestMethod.POST)
@ResponseBody
public String editPwd(HttpServletRequest request, String oldPassword, String newPassword) {
UserContext userContext = (UserContext) request.getSession().getAttribute("userContext");

UserInfo old = userFacade.findUserById(userContext.getUserId());

if (StringUtils.md5(oldPassword).equals(old.getPassword())) {
old.setPassword(StringUtils.md5(newPassword));
old.setUpdateTime(System.currentTimeMillis());

userFacade.editUser(old);
} else {
return FAIL;
}

return SUCCESS;
}
posted @ 2016-04-13 17:11  园芳宝贝  阅读(8952)  评论(0编辑  收藏  举报