使用ajax请求,模态框调用并更改密码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ajax请求,模态框调用并更改密码相关的知识,希望对你有一定的参考价值。

前端页面

<a href="javascript:void(0);" onclick="changPassword()">
  <i class="ace-icon fa fa-cog"></i>
  修改密码
</a>

<script type="text/javascript">
  //打开模态框
  function changPassword(){
    $("#navbar_edit").modal({
      backdrop :‘static‘
    });
  }
</script>
<#include "/include/navbar_edit.html">

 

<!-- 模态框(Modal) -->
<div class="modal fade" id="navbar_edit" tabindex="-1" role="dialog"
  aria-labelledby="roleEditLabel" aria-hidden="true">
  <div class="modal-dialog" style="width:500px;">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" id="roleEditLabel">修改密码</h4>
      </div>
      <div class="modal-body">
      <div style="width:90%;">
        <form class="form-horizontal" id="navbar_pwd_reset">
          <div class="form-group">
            <label class="col-sm-4 control-label" for="old_password">旧密码:</label>
            <div class="col-sm-8" style="padding:0">
            <input class="col-sm-12" type="password" id="old_password"
              name="old_password" placeholder="请输入旧密码 " />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label" for="new_password">新密码:</label>
            <div class="col-sm-8" style="padding:0">
            <input class="col-sm-12" type="password" id="new_password"
              name="new_password" placeholder="请输入新密码 " />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label" for="confirm_password">确认新密码:</label>
            <div class="col-sm-8" style="padding:0">
              <input class="col-sm-12" type="password" id="confirm_password"
                name="confirm_password" placeholder="确认新密码 " />
            </div>
          </div>
        </form>
      </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-xs btn-default"
          data-dismiss="modal" id="navbar_close_btn">
        <i class="ace-icon fa fa-times bigger-110"></i> <span
          class="bigger-110 no-text-shadow">关闭</span>
      </button>
      <button type="button" class="btn btn-xs btn-primary" id="navbar_save_btn">
        <i class="ace-icon fa fa-floppy-o bigger-110"></i> <span
          class="bigger-110 no-text-shadow">保存</span>
      </button>
    </div>
   </div>
    <!-- /.modal-content -->
  </div>
<!-- /.modal -->
</div>

<script type="text/javascript">
  $("#navbar_edit").on("show.bs.modal", function() {
    $("#navbar_edit #navbar_pwd_reset").formClear();

  });
  $("#navbar_edit").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");

  });
  $(document).on("click", "#navbar_save_btn", function(e) {
    var old_psd = $("#navbar_pwd_reset #old_password").val();
    var new_psd = $("#navbar_pwd_reset #new_password").val();
    var con_psd = $("#navbar_pwd_reset #confirm_password").val();
    //alert(old_psd);
    //alert(new_psd);
    //alert(con_psd);
    if (old_psd == "" || new_psd == "" || con_psd == "") {
    bootbox.alert("相关字段不为空,请输入!");
      return false;
    };
    if (old_psd == new_psd) {
    bootbox.alert("旧密码和新密码相同,请重新输入!");
      return false;
    };
    if (new_psd != con_psd) {
      bootbox.alert("两次输入新密码不相同,请重新输入!");
      return false;
    };
    var params = $("#navbar_pwd_reset").serialize();
      //alert(params);
      $.ajax({
        cache : true,
        type : "POST",
        url : "${ctx!}/modifyPassword",
        data : params,
        async : false,
        error : function(request) {
          bootbox.alert("服务器连接错误,请稍后再试");
        },
        success : function(data) {
          if (SQ.isOK(data)) {
            bootbox.alert("重置成功", function() {
              $("#navbar_close_btn").click();
            });
          } else {
            bootbox.alert("重置失败:" + SQ.getMessage(data));
          }
        },
      });
    });
</script>

后端页面

public void modifyPassword() {
  BasMember sysUser = this.getLoginUser();

  if (sysUser == null || sysUser.getInt(BasMember.IsDelete).equals(1)) {
    render(new JsonRender(new AjaxResult(Constants.RESPONSE_STATUS_FAIL, "会话已过期,请重新登录!")).forIE());
    return;
  }

  // 旧秘密
  String password = getPara("old_password");
  password = CommonUtil.getEncryptedPwd(password);
  if (!password.equals(sysUser.get(BasMember.Password))) {
    render(new JsonRender(new AjaxResult(Constants.RESPONSE_STATUS_FAIL, "旧密码不正确!")).forIE());
    return;
  }

  // 新密码
  password = getPara("new_password");
  password = CommonUtil.getEncryptedPwd(password);

  // 赋值
  sysUser.set(BasMember.Password, password);
  sysUser.set(BasMember.ModifyBy, this.getLoginId());
  sysUser.set(BasMember.ModifyTime, StringUtil.getNowTime());
  if (!sysUser.update()) {
    render(new JsonRender(new AjaxResult(Constants.RESPONSE_STATUS_FAIL, "修改时出现异常")).forIE());
  }
  render(new JsonRender(new AjaxResult(Constants.RESPONSE_STATUS_OK, "修改成功!")).forIE());
}

 

以上是关于使用ajax请求,模态框调用并更改密码的主要内容,如果未能解决你的问题,请参考以下文章

使用 WordPress REST API 通过 AJAX 更改密码

使用 WCF 和 DefaultCredentials 时如何处理密码更改?

替换 Windows 更改密码提供程序的功能和 UI

十$.ajax模态/非模态框window.open()href属性submit()等提交请求及优劣及问题解决

键盘输入时,如何在Ajax调用URL更改后停止MVC页面刷新

如何以编程方式更改 Drupal 用户密码?