Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What HTTP response codes does the jQuery aJax error handler consider an 'error'?

I have a standard aJax call back to the server:

$.ajax({
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        url: '/Apps/ResetDateAndCount',
        data: JSON.stringify({
            appName: app
        }),
        success: function (response) {
            $('.lastReset').text(response);
            $('.currentCount').text('0');
        },
        error: function (xhr, status, error) {
            alert("You are not authorized to perform that action");
        }
    });

From my server (ASP.NET) I am returning errors like:

return Json(new { success = false, error = "You are not authorized to perform that action" });

and

Response.StatusCode = 401; return Json(new { success = false, error = "You are not authorized to perform that action" });

and

Response.StatusCode = 500; return Json(new { success = false, error = "You are not authorized to perform that action" });

Inside of the error handler error: function (xhr, status, error) only the last return will be caught as an error, when the status code is set to 500.

I am wondering what response codes aJax actually consider an "error"?

like image 832
sosil Avatar asked Jan 10 '17 20:01

sosil


People also ask

How does error handle response in Ajax?

When there is an AJAX error response or the AJAX request times out, you'll want to log as much information as you have, including the error message that jQuery gives you, the url and the request data. $. ajax(url, { "data": requestData, "type": "POST", "timeout": 5000 }) .

What is the meaning of 404 status code in Ajax?

The HTTP 404 status code indicates the resource or web page you are trying to access does not exist. Either it is removed from the website or you mistyped the URL. Like 500 error code, the 404 status code does not indicate that there is a problem with the server you are trying to access.

What does Ajax error 0 mean?

Status code 0 means the requested url is not reachable.


1 Answers

Any error code that is outside of the interval [200, 299] and different than 304 is considered an error by jQuery when making an AJAX call.

Now of course you're gonna ask me, that's fine and dandy but why the hell this is not considered as an error, after all I am returning 401 which outside of the aforementioned interval so it should be considered an error:

Response.StatusCode = 401; 
return Json(new { success = false, error = "You are not authorized to perform that action" });

Very simple: because when you return a 401 status code from a controller action in ASP.NET MVC, the framework (and more specifically the ASP.NET Forms Authentication module) will intercept this request and redirect you to the login form eventually resulting in a 200 status code. Yeah, the login page is served with a 200 OK status code. In this case 200 is inside the interval so no error callback is invoked. You can validate this by inspecting the DOM of the login page returned in your success callback for this particular case.

Now put yourself into the perspective of a browser making an AJAX request: It will follow all the redirects that a server makes until it reaches the final destination. If this final destination status code is outside of the interval and different than 304 then you will get the error callback invoked. OK, now things start to make a little more sense.

So the next question you're gonna ask me is how do I invoke the error callback if I return a 401 status code from my controller action, right? Then I will redirect you (:-)) to the following blog post: Prevent Forms Authentication Login Page Redirect When You Don't Want It.

like image 180
Darin Dimitrov Avatar answered Oct 10 '22 20:10

Darin Dimitrov