Unable to throw exception on ajax call from controller
我希望在ajax调用的客户端抛出异常,但我没有状态代码。
我做了什么来处理ajax错误函数的错误。
当我调用ajax函数时,它会在SaveAccount Controller上生成一些异常,我通过HandleExceptionAttribute处理它,最后我返回json但是它遇到成功函数,错误是什么问题。
请帮忙。 谢谢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class HandleExceptionAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null) { filterContext.Result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { filterContext.Exception.Message, } }; filterContext.ExceptionHandled = true; } else { base.OnException(filterContext); } } } [HandleExceptionAttribute] [AllowAnonymous] [HttpPost] public ActionResult SaveAccount(AccountViewModel vm) { try { using (var context = new AdvisorFinancialEntities()) { .......... context.SaveChanges(); } } catch (Exception ex) { Exception e = new Exception("Failed to save account"); e.Data.Add("Accounts","SaveAccount"); throw e; } return Json(true, JsonRequestBehavior.AllowGet); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $.ajax({ url: '@Url.Action("SaveAccount","Accounts")', cache: false, type: 'POST', contentType: 'application/json', data: jsonData, success: function (result) { ShowToastMessage("Account has been saved successfully","Success",true); Reset(); }, error: function (xhr, textStatus, errorThrown) { var err = JSON.parse(xhr.responseText); ShowToastMessage(err.Message,"Save Account",false); } }); |
由于您使用
您可以在