There are similar questions asking about this but the solutions doesn't seem to work for me. I have a Java application that HTTP GETs a server for retrieving some information but I am testing situations in which this server is not responding. I am trying to reach a web server that returns HTTP 403 (forbidden) response and I would like to handle the error.
First with the following code I am able to do HTTP GET, reach the web page and that's it. I do not get the HTTP 403 Error response. However, if I uncoment the timeout in the following code ajax will be able to reach error and respond with 'timeout' error. Yes, it is a timeout error but due to a HTTP 403 error, I would like to get the 403.
init: function (credentials) {
$.ajax({
type: "GET",
url: this.serverURL,
//timeout: 5000,
headers: {
Accept: "application/json"
},
contentType: "application/json; charset=utf-8",
crossDomain: true,
cache: false,
data: {
'username': credentials.username,
'key': credentials.key
},
jsonp: false,
dataType: 'jsonp',
//never enters here...
statusCode: {
404: function() {
alert('404 page not found');
},
400: function() {
alert('400 bad request');
},
403: function() {
alert('403 forbbiden');
}},
error: function (XMLHttpRequest, errorThrown) {
handleError(XMLHttpRequest, errorThrown);
}
});
function handleError( XMLHttpRequest, errorThrown ) {
//If not found, internal server error or forbidden (it should enter here)
if (XMLHttpRequest.status == 404 || XMLHttpRequest.status == 500 || XMLHttpRequest.status == 403) {
Logger.loginfo('never enters here...');
}
else if (XMLHttpRequest.readyState == 4) {
//http error
Logger.loginfo('Error: ' + errorThrown);
}
else if (XMLHttpRequest.readyState == 0) {
// Network error, not possible to open() (i.e. connection refused, access denied...)
Logger.loginfo('Error: ' + errorThrown);
}
else {
//http error
Logger.loginfo('Error else!: ' + errorThrown);
// something after initializing/opening the HTTP GET but before completing it
}
};}
Any help would be very much appreciated.