The login process for my app is functioning correctly. It successfully connects to the server and returns a 200 Status, indicating that it is working fine. However, after the service completes its task, the controller does not execute its lines of code:
Controller:
loginService.signin(formData, function(res) {
console.log('This message never prints');
});
Any ideas on how to resolve this issue? It's possible that the interceptor may be causing this behavior. Here are the relevant scripts:
app.js
'use strict';
var app = angular.module('myapp', [
'ngRoute',
'ngStorage',
'ngCookies',
'myapp.controllers',
'myapp.services'
]);
app.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/logueo.html',
controller: 'LoginController'
})
.when('/principal', {
templateUrl: 'views/principal.html'
})
.otherwise({
redirectTo: '/'
});
}]);
controller.js
angular.module('myapp.controllers', [])
.controller('LoginController',
['$rootScope',
'$scope',
'$http',
'$location',
'$localStorage',
'loginService',
'$cookies',
function ($rootScope, $scope, $http, $location, $localStorage, loginService, $cookies) {
$scope.signin = function() {
var formData = {
username: $scope.username,
password: $scope.password
};
loginService.signin(formData, function(res) {
// ISSUE HERE.
// IT DOES NOT EXECUTE THE REMAINING LINES.
// Incorrect user.
if (res.type == false) {
console.log("FALSE " + res.data);
}
// Correct user.
else {
window.location = "#/principal.html";
}
}, function() {
$rootScope.error = "Error en el logueo del usuario";
});
}
}]);
service.js
'use strict';
angular.module('myapp.services', [])
.factory('loginService', ['$http', '$localStorage', 'tokenStorage',
function ($http, $localStorage, tokenStorage) {
// THIS IS WORKING PERFECTLY FINE
return {
signin: function(data, success, error) {
$http.post(
'http://myweb.com/api' + '/login',
data
).success( function (result, status, headers) {
console.log("logged in");
}).error(function (data, status, headers, config) {
console.log("error. " + data);
})
}
};
}]);