1

我在我的 asp.net mvc Web 应用程序中使用 Jquery UI 选项卡。我的标签工作得很好。

但是,问题是当发生 ajax 错误时,应该捕获它并返回 JSON 响应。

我正在使用一个 CustomError 处理程序来覆盖 MVC HandleError 属性,如下所示:

public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }

            if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
            {
                return;
            }

            if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
            {
                return;
            }

            // if the request is AJAX return JSON else view.
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                filterContext.Result = new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new
                    {
                        error = true,
                        message = filterContext.Exception.Message
                    }
                };
            }
            else
            {
                var controllerName = (string)filterContext.RouteData.Values["controller"];
                var actionName = (string)filterContext.RouteData.Values["action"];
                var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

                filterContext.Result = new ViewResult
                {
                    ViewName = View,
                    MasterName = Master,
                    ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                    TempData = filterContext.Controller.TempData
                };
            }
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }
    }

因此,如果发生错误并且是 ajax 请求,那么上述方法将抛出 JSON 响应。

但是,我正在努力找出如何捕捉 JSON 响应并在客户端显示它。

请帮助..我尝试将 ajaxoptions 与 UI 选项卡一起使用,如下所示:

 $(document).ready(function () {
            $('#tabs').tabs({
                activate: function (event, ui) {
                    ui.oldPanel.empty();
                },
                ajaxOptions: { success: Success, error: Failure }
            });
            $('#tabs').css('display', 'block');
            $(function () {
                $(this).ajaxStart(function () {
                    $("#ajaxLoading").show();
                });
                $(this).ajaxStop(function () {
                    $("#ajaxLoading").hide();
                });
            });
        });

        function Success(data) {
            alert("Successfully loaded the tabs");
        }

        function Failure() {
            alert("Some thing wrong had happened");
        }

请帮助..关于如何接收错误的 JSON 响应并向最终用户显示适当的警报..

4

1 回答 1

0

我找到了这样的解决方案:

$.ajaxSetup({
    type: "GET",
    cache: false,
    error: function (e) {
        var Error = e.responseText;
        var ErrorCode= xx;
        alert("Sorry, An Error has been occured while processing your request " + Error);
    }
});

我曾经ajaxSetup()收到来自服务器端的响应。

希望这可以帮助...

于 2013-11-18T12:58:01.070 回答