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 エラー応答を設定していましたが、

しかし、問題は、xhr.responseText を使用するのではなく、クライアント側でその json をキャプチャしてそのエラー メッセージを表示できなかったことです。

私は次の方法で試しました:しかし、これは動作しません:

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

アラートは次のように送信されます:

some thing wrong has happened+ **undefined**

エラーメッセージは表示されず、未定義として表示されます。これは、CustomError ハンドラーからの json 応答が受信されていないことを意味します。

上記の実装方法について、ご協力またはご意見をお寄せください。

4

1 に答える 1

1

コールバックには、 jQuery.ajaxのコールバックloadErrorと同じパラメータが含まれます:タイプjqXHR ( XMLHTTPRequestオブジェクトのスーパーセット)、 stringおよび string 。したがって、JSON 応答にアクセスするには、明示的に呼び出す必要があります。またはの場合にのみ、サーバーからのエラー応答を JSON 応答として解釈しようとすることをさらに検証するために、 inside を使用できます。最初にが文字列を返すことを確認し( orではない)、次に を使用してなしで部分を取得できます。errorjqXHRtextStatuserrorThrown$.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 に答える