我正在尝试使用angular js和rest webservice创建一个简单的登录表单。 我可以调用其余的webservice,但无法获得响应。
1)登录表格:
<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;
});
};
});
这里的respose.data是未定义的。
任何人都可以帮我解决问题吗?
答案 0 :(得分:1)
你不能在这里覆盖响应。
您可以为此对象添加更多属性,例如:
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
});
};
});