Is there a better way to rewrite my ajax call please

Is there a better way to rewrite my ajax call please

Hello,

I am using jquery with an API, I need to output different error messages based on the response.

I would like to know if there is a way to rewrite my ajax call in a more efficient way please, why?

1)I would like to shrink my responses with only 4 possible outputs ":
200
401
500
and else(a global for all the other errors). <--- how to default any other errors to one single response?

Also, I have noticed that when the API is down and the the ajax returns error 503, I am not able to catch this error in a 503 response nor the "error:" response. Any idea why please?


$ . ajax ({
url : SERVER_URL + "/oauth/token" ,
method : "POST" ,
data : {
grant_type : "refresh_token" ,
client_id : CLIENT_ID ,
client_secret : CLIENT_SECRET ,
refresh_token : refreshToken
},
timeout : 8000 ,
dataType : "json" ,
cache : false ,

beforeSend : function (xhr) {
//Nothing yet
},

error : function (data, textStatus) {
if (textStatus === "timeout" || textStatus === "error" ) {
console . log ( "checkRefreshToken - Ajax - Error" );
returnToLoginOnError ();
}
},

statusCode : {
//If status is 200

200 : function (response) {
checkAuthorizationAndRole (
response . access_token ,
response . refresh_token
);

},
401 : function (response) {
//Unauthenticated
returnToLoginOnError ();
console . log ( "checkRefreshToken - 401 - Unauthenticated" );
},
404 : function (response) {
//Bad Url
console . log ( response . message );

notificationSendingReport . close ();
notificationError404 . open ();
},
500 : function (response) {
//ERROR
console . log ( response . message );

notificationSendingReport . close ();
notificationError500 . open ();
},
503 : function (response) {
//ERROR
console . log ( response . message );

notificationSendingReport . close ();
notificationError500 . open ();
}
}
});
}


Thank you.