roneng
roneng

Reputation: 1

AngularJS RESTful POST response

I'm new to AngularJS. I have a simple java RESTful service that return a json. And a function in a Collector:

angular.module('testApp').controller('LoginController', function($scope, $http, $timeout, $resource) {

$scope.mapp_login_email = '';
$scope.mapp_login_password='';  
$scope.loginAPI = function() {
    var baseUrl = 'http://localhost:8080';
    $scope.loginAPI = $resource(baseUrl + '/api/login/post', {}, {
        login2: {method: 'POST'}
    });        
    var response = $scope.loginAPI.login2({ email: $scope.mapp_login_email, password: $scope.mapp_login_password });

    response.$promise.then(function(value) {        
        var data = JSON.stringify(value, ['returnCode', 'errMsg']); 
        $scope.mapp_login_email = data.returnCode;
    });     

  }

});

The RESTful service is being called and I see in the network tab that it returns a json response:

{returnCode: "OK", status: 0, errMsg: null}

How can I evaluated in the angularJS code the return value from the RESTful service?

Thanks, Ronen

Upvotes: 0

Views: 181

Answers (2)

maleeb
maleeb

Reputation: 920

There is a cleaner way to use HTTP with angular. Try the $http service.

var baseUrl = 'http://localhost:8080';
$http
.post(baseUrl + '/api/login/post', null, { 
    email: $scope.mapp_login_email, 
    password: $scope.mapp_login_password 
})
.then(function successCallback(response) {
    // on success
    console.log(response);
}, function errorCallback(response) {
    // on error
    console.log(response);
});

Upvotes: 1

pradeep1991singh
pradeep1991singh

Reputation: 8365

You can handle promise in then clause

response.$promise.then(function(value) {
    $scope.data = value;
});

Upvotes: 0

Related Questions