0

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.

agonza1
  • 439
  • 7
  • 20

1 Answers1

0

You should add the following in the exception and check if you are getting all the values back properly.

error: function(xhr, textStatus, error){
  console.log(xhr.statusText);
  console.log(textStatus);
  console.log(error);
  //then call your function and pass the values 
  //and do the checks
}

Also you can check this answer for other details.

Community
  • 1
  • 1
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 1
    Hi Pritam, I am getting timeout error in the 3 logs.... If I don't use timeout I will never read any log. I will keep looking, thank you for the link. – agonza1 Dec 05 '16 at 18:56