1

I have the services and within particular time duration if response is come than ok, other wise show error in popup.

Here is my service code:

angular.module('server', [])

.factory('api', function($http) {
    var server = "http://myapi-nethealth.azurewebsites.net";
        return {
            //Login
            login : function(formdata) {
                return $http({
                    method: 'POST',
                    url: server + '/Users/Login',
                    data: $.param(formdata),
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded'},
                })
            },
        };

    });

Please tell me how can I use timeout property in services.

Preety Sapra
  • 615
  • 1
  • 6
  • 18
  • try to use `timeout` option from `$http` API https://docs.angularjs.org/api/ng/service/$http#usage – monkeyinsight Oct 02 '14 at 05:01
  • How can I put pop after time out, I have to put in controller or service only... – Preety Sapra Oct 02 '14 at 05:07
  • RTFM "Returns a promise object with the standard then method and two http specific methods: success and error." you just need `$http({your options}).success(function(){alert('success')}).error(function(){alert('error')})` – monkeyinsight Oct 02 '14 at 05:10
  • I don'y know how use the promise, I am using the services in the simple way that I have post in question, can you please tell me how can I use this in my code.....I am new to angularjs – Preety Sapra Oct 02 '14 at 05:12

1 Answers1

1

Read this post - How to set a global http timeout in AngularJs

Where you can set a timeout for your http calls.

You can inject the above factory in your controller and then make a call with success and error callbacks like below

api.login(formdata)
.success(function(){ alert("success"); })
.error(function(){ alert("error"); });
Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281