ignaciotr
ignaciotr

Reputation: 198

Success callback in $http promise isn't happening

I'm doing an angular app that consumes a RESTful API. I'm handling the authentication of the user this way:

// in services.js

app.factory('authService', ['$http', '$q', 'localStorageService', 
  function ($http, $q, localStorageService) {

    var login = function (loginData) {

      var data = "grant_type=something&username=" + loginData.userName + "&password=" + loginData.password;
      var deferred = $q.defer();

      $http.post(serviceBase + 'token', data).
        success(function (response) {
          localStorageService.set('authorizationData', { token: response.access_token });
          localStorageService.set('personaData', { username: response.username });

          deferred.resolve(response);
        }).
        error(function(err, status) {
          deferred.reject(err);
        });

      return deferred.promise;

    };

And the controller

// controller.js

app.controller('LoginCtrl', ['$scope', '$state', 'authService',
  function ($scope, $state, authService) {
    $scope.loginData = {
      userName: '',
      password: ''
    };

    $scope.alert = {type: '',msg: ''};

    $scope.login = function () {
      authService.login($scope.loginData).
        then(function (response) {
          $state.go('index'); // when the service verify that the auth data is correct, the controller redirects to the principal state 
        }, function (err) {
          $scope.alert = {
            type: 'danger',
            msg: 'non'
          };
        });
    };
  }
]);

The problem is when the user sends the correct auth info (meaning username and password), the service does not go through the .success callback when the response is 200 OK. And yes, this behaviour is only expected when the response isn't 200 OK.

The exact response of the resource is:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 851
Content-Type: application/json;charset=UTF-8
Expires: -1
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Wed, 11 Mar 2015 16:20:12 GMT

{"access_token":"...","token_type":"...","expires_in":"...","username":"...","role":"...","display_name":"..."}

What's happening here? How can I solve this? Thanks.

Upvotes: 1

Views: 86

Answers (1)

Brad Barber
Brad Barber

Reputation: 2963

You don't need to create a new promise, you can just return the promise from the $http call - I think an exception is occurring in your call to localStorageService but it's getting lost and your promise is never resolved. You can change your authService to the following and hopefully you'll see the error in localStorageService:

app.factory('authService', ['$http', '$q', 'localStorageService', 
  function ($http, $q, localStorageService) {

  var login = function (loginData) {

    var data = "grant_type=something&username=" + loginData.userName + "&password=" + loginData.password;

    return $http.post(serviceBase + 'token', data).
      success(function (response) {
        localStorageService.set('authorizationData', { token: response.access_token });
        localStorageService.set('personaData', { username: response.username });

        return response;
    });
  };

Upvotes: 1

Related Questions