2011-03-02 34 views
0

在我忘记密码的形式,jQuery的AJAX后触发多次(每次使用后多了一个提交)。因此,如果用户输入3次无效的电子邮件,然后输入有效的电子邮件,则用户将获得4个密码更改的电子邮件。似乎每次发生错误时都会堆叠事件,所有这些事件都会在下次提交时被解雇。这是我所有的相关代码。我究竟做错了什么?

JS

RetrievePassword = function() { 
     var $popup = $("#fancybox-outer"); 
     var form = $popup.find("form"); 
     form.submit(function (e) { 
      var data = form.serialize(); 
      var url = form.attr('action'); 
      $.ajax({ 
       type: "POST", 
       url: url, 
       data: data, 
       dataType: "json", 
       success: function (response) { 
        ShowMessageBar(response.Message); 
        $.fancybox.close() 
       }, 
       error: function (xhr, status, error) { 
        ShowMessageBar(xhr.statusText); 
       } 
      }); 
      return false; 
     });  
    }; 

MVC控制器/ ACTION

[HandlerErrorWithAjaxFilter, HttpPost] 
     public ActionResult RetrievePassword(string email) 
     { 
      User user = _userRepository.GetByEmail(email); 

      if (user == null) 
       throw new ClientException("The email you entered does not exist in our system. Please enter the email address you used to sign up."); 

      string randomString = SecurityHelper.GenerateRandomString(); 
      user.Password = SecurityHelper.GetMD5Bytes(randomString); 
      _userRepository.Save(); 

      EmailHelper.SendPasswordByEmail(randomString); 

      if (Request.IsAjaxRequest()) 
       return Json(new JsonAuth { Success = true, Message = "Your password was reset successfully. We've emailed you your new password.", ReturnUrl = "/Home/" }); 
      else 
       return View();   
     } 

HandlerErrorWithAjaxFilter

public class HandleErrorWithAjaxFilter : HandleErrorAttribute 
    { 
     public bool ShowStackTraceIfNotDebug { get; set; } 
     public string ErrorMessage { get; set; } 

     public override void OnException(ExceptionContext filterContext) 
     { 
      if (filterContext.HttpContext.Request.IsAjaxRequest()) 
      { 
       var content = ShowStackTraceIfNotDebug || filterContext.HttpContext.IsDebuggingEnabled ? filterContext.Exception.StackTrace : string.Empty; 
       filterContext.Result = new ContentResult 
       { 
        ContentType = "text/plain", 
        Content = content 
       }; 

       string message = string.Empty; 
       if (!filterContext.Controller.ViewData.ModelState.IsValid) 
        message = GetModeStateErrorMessage(filterContext); 
       else if (filterContext.Exception is ClientException) 
        message = filterContext.Exception.Message.Replace("\r", " ").Replace("\n", " "); 
       else if (!string.IsNullOrEmpty(ErrorMessage)) 
        message = ErrorMessage; 
       else 
        message = "An error occured while attemting to perform the last action. Sorry for the inconvenience."; 

       filterContext.HttpContext.Response.Status = "500 " + message; 
       filterContext.ExceptionHandled = true; 
       filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
      } 
      else 
      { 
       base.OnException(filterContext); 
      } 
     } 

     private string GetModeStateErrorMessage(ExceptionContext filterContext) 
     { 
      string errorMessage = string.Empty; 
      foreach (var key in filterContext.Controller.ViewData.ModelState.Keys) 
      { 
       var error = filterContext.Controller.ViewData.ModelState[key].Errors.FirstOrDefault(); 
       if (error != null) 
       { 
        if (string.IsNullOrEmpty(errorMessage)) 
         errorMessage = error.ErrorMessage; 
        else 
         errorMessage = string.Format("{0}, {1}", errorMessage, error.ErrorMessage); 
       } 
      } 

      return errorMessage; 
     } 

    } 

这里有更多的JS。我使用fancybox插件作为我的灯箱。 “检索密码”链接位于登录灯箱中。

$(document).ready(function() { 

    $(".login").fancybox({ 
     'hideOnContentClick': false, 
     'titleShow': false, 
     'onComplete': function() {  

      $("#signup").fancybox({ 
       'hideOnContentClick': false, 
       'titleShow':false, 
       'onComplete': function() { 

       } 
      }); 

      $("#retrievepassword").fancybox({ 
       'hideOnContentClick': false, 
       'titleShow': false, 
       'onComplete': function() { 

       } 
      }); 
     } 
    }); 

}); 
+0

你能发布更多的JavaScript代码吗?最可能发生的事情是,每次实际提交时,意外情况都会重新绑定提交代码。这发生在我身上几次。 – jfar 2011-03-02 18:20:33

+0

谢谢,我增加了一些JS ...... – Prabhu 2011-03-02 18:31:17

回答

0

我不是一个jQuery的专家,但它似乎对我来说,每一次的fancybox的onComplete火灾,添加(另一个)事件处理程序#retrievepassword和#signup ......我失去了休息在页面中的HTML,所以我不知道是否/何时登录对话框内容加载,但以下可能会更好地工作:

$(document).ready(function() { 

    $(".login").fancybox({ 
     'hideOnContentClick': false, 
     'titleShow': false, 
     'onComplete': function() {  

     } 
    }); 

    $("#signup").fancybox({ 
     'hideOnContentClick': false, 
     'titleShow':false, 
     'onComplete': function() { 

     } 
    }); 

    $("#retrievepassword").fancybox({ 
     'hideOnContentClick': false, 
     'titleShow': false, 
     'onComplete': function() { 

     } 
    }); 
}); 
+0

顺便说一句 - 我_really_喜欢你HandleErrorWithAjaxFilter ......我有可能借有一天... – 2011-03-02 19:36:51

+0

感谢你的小费。顺便说一句,我从这里得到了HandleErrorWithAjaxFilter:http://erikzaadi.com/blog/2009/12/22/AspdotNETMVCExceptionHandlingWithJQuery.xhtml我不太确定的是,如果在那里标记模型状态错误是一个好主意并抛出模型状态错误作为应用程序异常。我在这里发布有关此:http://stackoverflow.com/questions/5171684/whats-the-recommended-way-to-report-model-state-and-application-errors-to-client你的意见是什么? – Prabhu 2011-03-02 19:40:05

+0

我不确定,我不得不测试它。在Java被禁用的情况下,向后兼容性看起来很不错... – 2011-03-02 21:13:02

相关问题