비밀번호 분실 양식에서 jquery ajax 게시물이 여러 번 (각 제출 후 한 번 더) 해고됩니다. 따라서 사용자가 잘못된 이메일을 세 번 입력 한 다음 올바른 이메일을 입력하면 사용자는 비밀번호로 변경된 이메일을 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 CONTROLLER/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() {
}
});
}
});
});
더 많은 자바 스크립트 코드를 게시 할 수 있습니까? 가장 가능성있는 일은 실수로 제출할 때마다 제출 코드를 다시 바인딩하는 것입니다. 이것은 나에게 몇 번 일어났습니다. – jfar
고마워, 나는 더 많은 js를 추가했다 ... – Prabhu