0

I would like to add (default) action when an error occurs and it's not (403, 500 or 410 code):

    $.ajaxSetup({
        statusCode: {
            403: function () {
                window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash;
            },
            500: function() {
                window.location = '@Url.Action("AccessDenied", "Error")';
            },
            410: function() {
                window.location = '@Url.Action("Deleted", "Error")';
            }
            // ANY OTHER ERROR CODE - but it doesn't work, how can i do it?
            if not any above and it's an error then =>
            window.location = '@Url.Action("Index", "Error")';
        }
    });

2 Answers 2

2

You should be using ajaxError() to handle errors not ajaxSetup():

$(document).ajaxError(function(event, xhr, options) {
     switch (xhr.status)
     {
        case 403:
            window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash;
        break;
        case 500:
            window.location = '@Url.Action("AccessDenied", "Error")';
        break;
        case 410:
            window.location = '@Url.Action("Deleted", "Error")';
        break;
        default:
            window.location = '@Url.Action("Index", "Error")';
        break;
     }
   }
});
Sign up to request clarification or add additional context in comments.

5 Comments

but for some reason using ajaxError hit that function: please see: stackoverflow.com/questions/16080462/…
but still. ajaxError for some reason is never hit. stackoverflow.com/questions/16080462/…
@ShaneKm Did you try attaching it to $(document) like in my answer and not $("body") like in your question.
yes. i never get alert(IN) response. is it something wrong with my attribute? but as you can see in firebug response I do get the right response
I don't know, your function takes 4 parameters, the one I specify takes 3 (that's the one I use). Have you tried changing that?
0

The "complete" event is always fired .

complete: function(jqXHR, textStatus) {
    switch (jqXHR.status) {
        case 200:
            alert("error 200");
            break;
        case 404:
            alert("error 404");
            break;
        default:
            alert("DEFAULT");
    }
}

Comments

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.