11

I am making an ajax call using $http in angular js. I have implemented timeout in it. But I want to show the user an error message if the connection times out. The following is the code..

$http({
            method: 'POST',
            url: 'Link to be called',
            data: $.param({ 
                    key:Apikey,
                    id:cpnId
                }),
            timeout : 5000, 
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(result){
               alert(result);
            }).error(function(data){
              alert(data);
            });

Is there any way so that I can display the user if the connection is timed out. Is there any way so that it can be configured at one place?

Swagat Swain
  • 495
  • 1
  • 4
  • 22

4 Answers4

23

And if you want to do someting when connection timed out for every request, you can use interceptors (global timeout param doesn't work) :

// loading for each http request
app.config(function ($httpProvider) {
    $httpProvider.interceptors.push(function ($rootScope, $q) {
        return {
            request: function (config) {
                config.timeout = 1000;
                return config;
            },
            responseError: function (rejection) {
                switch (rejection.status){
                    case 408 :
                        console.log('connection timed out');
                        break;
                }
                return $q.reject(rejection);
            }
        }
    })
})
Nicolas Janel
  • 3,025
  • 1
  • 28
  • 31
  • Hi man, I got the rejection.status == 0 when the request timed out, and the other network error status also 0 – Raymond Liao May 09 '17 at 05:51
  • 3
    using angular 1.5.11 I get status code -1, any Idea where can I find official documentation on this value? – Rathma May 10 '17 at 11:07
1

Have a try on this blog page: http://www.jonhartmann.com/index.cfm/2014/7/23/jsFiddle-Example-Proper-Timeout-Handling-with-AngularJS It has a complete angular running example which resolve your question.

Phuong Ha
  • 31
  • 2
  • 5
    Its better to give some explanation and provide link for more information. This way it will be helpful in case the link is not working. – Vamshi Suram Dec 03 '14 at 14:37
  • 1
    This isn't a good example because sets timedOut in caller code, still don't know how to detect when a real http time out occurred. – user3285954 May 27 '15 at 15:04
  • 1
    This is a good answer. The timeout angular is looking for is always referring to the caller code anyway, so I don't know what @user3285954 is talking about. This example demonstrates use of a promise as the timeout value, where you can insert extra data to track which timeout got tripped. Nice. – JoshuaDavid Mar 16 '16 at 18:10
  • 1
    One flaw: this answer should have used $timeout instead of setTimeout – JoshuaDavid Mar 16 '16 at 18:31
1

You can use angular interceptor to achieve this.

$httpProvider.responseInterceptors
.push(['$q', '$injector','$rootScope', function ( $q, $injector,$rootScope) {
 return function (promise) {
    return promise.then(function (response) {
        return response;
    }, function (response) {
        console.log(response); // response status
        return $q.reject(response);
    });
  };
 }]);
}]);

More info see this link

Prashobh
  • 9,216
  • 15
  • 61
  • 91
-2

You just need to check the response status and that's it :

}).error(function(data, status, header, config) {
      if (status === 408) {
          console.log("Error - " + status + ", Response Timeout.");
      } 

});

For global HTTP timeout, take a look at this answer

Community
  • 1
  • 1
Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
  • 1
    This is not a good answer. Where are you getting the 408 from? You cannot assume the timeout will result in a status being delivered, i.e., status could be 0. – JoshuaDavid Mar 16 '16 at 18:06
  • 1
    This is an incorrect answer. The status code will NOT be equal to 408. – Lukkha Coder Mar 29 '16 at 23:20