1

I noticed that I can use the $http provider in this way:

Auth.login(userdetails).then(function(response){
//...
}

app.service('Auth', ['$http', function ($http) {

    AuthService.login = function (credentials) {

        return $http.
            post('getauth.php', credentials).
            then(function (res) {

                //create the session, etc.

            });

}]);

Notice the return statement in front of http and the use of then() on http instead of success().

Why does this work? Are there any downsides?

reggie
  • 3,523
  • 14
  • 62
  • 97
  • possible duplicate [Angular HTTPPromise](http://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a) – Anubhav Dec 04 '14 at 07:59
  • I just answered this http://stackoverflow.com/questions/27287731/angular-http-service-success-error-then-methods/27288310#27288310 – Chandermani Dec 04 '14 at 08:39

2 Answers2

0

Try to chain the promise like that:

$http.post('URL', DATA)
    .success(function(data, status, headers, config)) {}) // on success callback
    .error(function(data, status, headers, config) {}); // on error callback
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
0
 $http({
        url: 'put url here',
        method: "put action here just like GET/POST",
        data: { 'name': 'Anil Singh' }
    })
    .then(function (resp) {
        //TODO: put success logic here
    },
    function (resp) {
        //TODO:  put failed logic here
    }
 );

Angular $http service- success(), error(), then() methods

Community
  • 1
  • 1
Anil Singh
  • 4,173
  • 4
  • 41
  • 47