簡體   English   中英

在$ .Ajax錯誤事件中從XMLHttpRequest檢索數據

[英]Retrieving data from XMLHttpRequest on $.Ajax Error Event

如標題所示,我想從XMLHttpRequest檢索我的Json數據值。 我已經知道如何通過使用常見的$ .Ajax成功事件來做到這一點,但是我想從錯誤事件中獲取它的值。 我注意到,要找到各種XMLHttpRequest類型並不容易。

為了進一步說明,這里是這種情況:在不活動之后,用戶會話到期。 如果他嘗試使用Ajax調用進行任何操作,他將被重定向到登錄頁面。 我在實現OnException方法的特定過濾器上處理此會話超時錯誤。

雖然我可以使用某種技巧(通過手動設置HTTP響應代碼)來做到這一點,但我還是想以“正確的方式”做到這一點,而沒有技巧。

OnException方法代碼段

filterContext.ExceptionHandled = true;

        // If this is an ajax request, return the exception in the response
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            //If the session has expired, throws a SESSION TIMEOUT error
            if (Session["UserLogged"] == null)
            {
                //The hack mentioned before
                filterContext.HttpContext.Response.StatusCode = 502;
                filterContext.Result = new JsonResult()
                {
                    Data = new { success = false, errorType = "SESSION_TIMEOUT" ,error = filterContext.Exception.ToString() },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };  
            }

}

因此,通過使用簡單的$ .Ajax錯誤事件,我將如何從filterContext.Result檢索數據? 特別是errorType參數。

您應該能夠從錯誤處理程序中的jqXHR responseText屬性中解析出JSON數據。

error: function(jqXHR, textStatus, errorThrown) {
          alert('jqXHR.responseText = ' + jqXHR.responseText);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM