0

I wrote a controller for login page. Here is my controller:

var authApp = angular.module('loginApp', [])

authApp.controller('LoginCtrl', ['$scope', '$location', 'loginFactory', function($scope, $location, loginFactory){
    $scope.authenticate = function() {
        loginFactory.login($scope.username, $scope.password)
        .then(function(response) {
            console.log(response.$statusText);
        }, function errorCallBack(response) {
            console.log(response.$statusText);
        });
    }

}]);

My service:

authApp.factory("loginFactory", function ($http) {
    return{
        login: function(username, password) {
            var data = "username="+username+"&password="+password+"&submit=Login";
            return $http({
                method: 'POST',
                url: 'http://localhost:8080/login',
                data: data,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            });  
        }

When I debug the code, authentication seems successful and it did get into then function. However nothing displays in console. And I got a warning(?) saying undefined for the line console.log(response.$statusText);. It is not an error since it is not red. Why doesn't it print out anything?

4
  • 1
    console.log must have displayed the result of response.$statusText as undefined.. Commented May 1, 2016 at 2:51
  • @Rayon It is a post request, how could the response be undefined? Commented May 1, 2016 at 2:56
  • When you get something like this do console.log(response); so you can see everything in the object. Commented May 1, 2016 at 2:59
  • Every request will respond buddy! response object may not have the $statusText key.. Commented May 1, 2016 at 3:00

1 Answer 1

2

Use response.statusText not response.$statusText. The documentation for AngularJS $http requests lists statusText as one of the properties of the response object - https://docs.angularjs.org/api/ng/service/$http

Sign up to request clarification or add additional context in comments.

2 Comments

In the documentation for AngularJS http requests it lists statusText as one of the properties for response
Ooh yes! I was reading it as a response object from server.. You might want to add reference

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.