2

我正在开发 ASP.net MVC 2.0 应用程序并在其中使用 jqgrid。

我正在使用以下代码来处理加载网格的 ajax 请求期间的异常

 loadError:function(xhr,status,error)
{
  alert("some thing wrong has happened"+xhr.responseTest);
}

当在 ajax 请求期间引发错误时,这会显示警报。

但是,在这里我不想使用 xhr.responseTest 来显示错误描述,而是我使用自定义错误处理程序,它以以下方式覆盖 MVC 的默认 HandleError 属性:

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 = AjaxError(filterContext.Exception.Message, filterContext);                                                
                //filterContext.ExceptionHandled = true;
                //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;
        }

        protected JsonResult AjaxError(string message, ExceptionContext filterContext)          
        {
            if (String.IsNullOrEmpty(message))
            message = "Something went wrong while processing your request. Please refresh the page and try again.";            
           filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;          
           filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;                    
           return new JsonResult {   Data = new{ ErrorMessage = message }, ContentEncoding = System.Text.Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet};}
    }

我正在使用 [CustomHandleError] 过滤器来处理控制器操作的异常。

因为,可以看到请求是否是 ajax 请求,我正在抛出一些包含错误信息的 json 响应。(上述代码中的数据(错误信息)

我正在将确切的错误信息输入 Data 变量并设置适当的 json 错误响应,

但是,问题是我无法在客户端捕获该 json 并显示该错误消息,而不是使用 xhr.responseText。

我尝试了以下方式:但这不起作用:

loadError:function(Error)
    {
      alert("some thing wrong has happened"+Error.ErrorMessage);
    }

警报如下:

some thing wrong has happened+ **undefined**

它不显示错误消息,而是显示为未定义,这意味着未收到来自 CustomError Handler 的 json 响应

请帮助或就如何实施上述任何建议。

4

1 回答 1

1

回调loadError包含与jQuery.ajaxerror的回调相同的参数:类型为jqXHRXMLHTTPRequest对象的超集)、字符串和字符串。因此,要访问 JSON 响应,您应该显式调用。您可以使用inside of来额外验证您尝试将来自服务器的错误响应解释为 JSON 响应,仅当is或. 您可以首先验证返回字符串(而不是或),然后使用获取不带的部分。jqXHRtextStatuserrorThrown$.parseJSON(jqXHR.responseText)jqXHR.getResponseHeader("Content-Type")loadError"Content-Type""application/json""application/json; charset=utf-8"jqXHR.getResponseHeader("Content-Type")nullundefined.split(";")[0]"application/json"charset=utf-8

于 2013-11-27T09:40:52.870 回答