1

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.

1 Answer 1

1

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);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Added example to answer. From there you will need to parse the response for the json data. I'm not sure off the top of my head what the format would be.
Yep, you're right. I'll just a simple parse example to transform your answer on a full answer. But thanks anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.