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?

4 Answers 4

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);
            }
        }
    })
})
Sign up to request clarification or add additional context in comments.

2 Comments

Hi man, I got the rejection.status == 0 when the request timed out, and the other network error status also 0
using angular 1.5.11 I get status code -1, any Idea where can I find official documentation on this value?
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.

4 Comments

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.
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.
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.
One flaw: this answer should have used $timeout instead of setTimeout
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

Comments

-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

2 Comments

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.
This is an incorrect answer. The status code will NOT be equal to 408.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.