As the title says, i want to retrieve my Json data values from a XMLHttpRequest. I already know how to do it by using a common success $.Ajax success event, but i want to get it's values from an error event. A thing that i noticed is not simple to find all kinds of a XMLHttpRequest types.
To explain a little more, here's the scenario: after some inactivity, the user sessions expires. If he tries to do any operations using an Ajax call, he'll be redirected to the login page. I handle this session timeout error on a particular filter that implements an OnException method.
While i can do it using a sort of a hack (by setting manually the HTTP response code), i'd like to do it on "proper way", with no hacks.
OnException method code snippet
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
};
开发者_高级运维 }
}
So, by using a simple $.Ajax Error Event, how would i retrive the data from the filterContext.Result
? Specifically the errorType
parameter.
You should be able to parse the JSON data out of the jqXHR responseText property in your error handler.
error: function(jqXHR, textStatus, errorThrown) {
alert('jqXHR.responseText = ' + jqXHR.responseText);
}
精彩评论