Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handles other status codes in $.ajax request

In the code below, I'm handling for status code 200 and 401. What do I do if I want to direct control to a function that handles all codes apart from 200 and 401?

$.ajax({
    type: "POST",
    dataType: "json",
    data:POSTData,
    url: 'http://localhost/api/user/authenticate',
    statusCode: {
        200: function() {
            alert("ok");
        },
        401: function() {
            alert("Invalid Credentials");
        }
    }
});
like image 833
Noor Avatar asked Jan 19 '12 09:01

Noor


1 Answers

try something like this:

$.ajax({
    type: "POST",
    dataType: "json",
    data:POSTData,
    url: 'http://localhost/api/user/authenticate',
    complete: function(xhr, statusText){
        switch(xhr.status){
            case 200:
                alert("ok");
            case 401:
                alert("invalid credentials");
            ....
            etc
        }
    }
});
like image 127
ghstcode Avatar answered Sep 22 '22 07:09

ghstcode