Unknown
Unknown

Reputation: 2137

Angular js:Not able to get the response for the rest method

I am trying to create a simple login form using angular js and rest webservice. I am able to call the rest webservice, but not able to get the response.

1)Login form:

<div class="col-md-6 col-md-offset-3">
    <h2>Login</h2>
    <div class="alert alert-danger">Error</div>
    <form name="form"  role="form" method="post" data-ng-submit="login(credentials)"
        data-ng-controller="LoginController">
        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" class="form-control" data-ng-model="credentials.username" required />
            <span  class="help-block">Username is required</span>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" name="password" id="password" class="form-control" data-ng-model="credentials.password" required />
        </div>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Login</button>
            <img  src="img/ajax-loader-blue.gif" />
            <a href="#/register" class="btn btn-link">Register</a>
        </div>
    </form>
</div>

2)Restwebservice

@Path("/login")
public class LoginRestfulService {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Users getUserProfile(CredentialInBean credentials) {
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+" credentials: "+credentials.getUsername()+""+credentials.getPassword());
        UserService userService = new UserService();
        return userService.getUserProfile(credentials);
    }

}

3)

public Users getUserProfile(CredentialInBean credentials) {
        if(credentials!=null && credentials.getUsername().equals("Firstname")){
            Users user = new Users();
            user.setFirstName("Firstname");
            user.setLastName("lastname");
            return user; 
        }
        return null;
    }

4)LoginController

app.controller('LoginController', function($scope, $rootScope, AUTH_EVENTS,
        AuthService) {
    $scope.credentials = {
        username : '',
        password : ''
    };
    $scope.login = function(credentials) {
        AuthService.login(credentials).then(function(user) {
            $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
            $scope.currentUser = null;
            $scope.setCurrentUser = function(user) {
                $scope.currentUser = user;
            };
        }, function() {
            $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
        });
    };
})

5)AuthService

services.factory('AuthService', function($http, Session) {
    var authService = {};
    var response='';
    authService.login = function(credentials) {
        return $http.post('/aclf/rest/login', credentials).then(function(response) {
            if (response !== null) {
                response = { success: true };
            } else {
                response = { success: false, message: 'Username or password is incorrect' };
            }
            alert(response.data);
            return response.data;
        });
    };

});

Here respose.data comes as undefined.

Can any one help me to solve the issue?

Upvotes: 1

Views: 104

Answers (1)

Alvaro Silvino
Alvaro Silvino

Reputation: 9763

you can't override the response here.

For the best you can add more attributes to this object like:

services.factory('AuthService', function($http, Session) {
var authService = {};
var response='';
authService.login = function(credentials) {
    return $http.post('/aclf/rest/login', credentials).then(function(response) {
        if (response !== null) {
            response.success = true ;
        } else {
            response = {};
            response.success = false;
            response.message = 'Username or password is incorrect';
        }
        alert(response.data);//you will preserve the data 
        return response.data;//won't be undefined 
    });
};

});

Upvotes: 1

Related Questions