0

I have written a custom angular service that returns the session id after making a post-call to the WCF service. I can see on the chrome's network tab that the call has returned the session-id but when I try to return it through $http.post to the caller it returns "{}" object.

I've followed all the questions/answers related to this problem in stackoverflow, etc. but to no avail. I know there is a problem with the handling of promise that was returned by the post-call but I don't know and not able to pin-point what's wrong and where.

This is the custom service.

angApp.service('RPCService', function($http) {

    var url_login = 'http://xxxxxxx';

    this.Login = function(request) {

        return $http.post(url_login, request)
                            .then(function (result) {
                                return result.data;
                                },
                            function (data, status) {
                                console.log(data + status);
                            });

    };

This is the caller.

angApp.controller('exportController', ['$scope', 'RPCService', function ($scope, RPCService) {

    $scope.export = {};

    $scope.exportLogin = function(){

        var req = {
                    "SiteId" : $scope.export.xxx,
                    "UserName" : $scope.export.xxx,
                    "Password" : $scope.export.xxx
                    };

        $scope.export.sessionId = RPCService.Login(req);
    };

I expected the output as "SessionId" : "xxxxxxxx" but I am getting "{}"

3 Answers 3

1

$http.post returns a promise and that is what your service method is returning

Change

$scope.export.sessionId = RPCService.Login(req);

To

RPCService.Login(req).then(function(data){
   $scope.export.sessionId = data;
})
Sign up to request clarification or add additional context in comments.

2 Comments

I've already tried that and the error was the function doesn't have "then" function defined. I'm not in front my system but would share the actual error.
Thanks for the input.
1

The signature of the error handler is wrong:

ERRONEOUS

  return $http.post(url_login, request)
                        .then(function (result) {
                            return result.data;
                            },
                        function (data, status) {
                            console.log(data + status);
                        });

BETTER

return $http.post(url_login, request)
 .then(function (result) {
    return result.data;
 },
  function (response) {
    var data = response.data;
    var status = response.status;
    console.log(data + status);
    //IMPORTANT re-throw error
    throw response;
});

It is important to re-throw an error in a rejection handler. Otherwise the rejected promise will be converted to a fulfilled promise with a value of undefined.

2 Comments

Nope didn't work. It still shows the output as "{}".
IT worked after I added your error response callback function with one parameter and used the Login call with the .then construct to read the promise. Thanks, georgeawg, and @charlietfl for the inputs.
0

Adding the single parameter for error callback and then using the promise accessor with .then keyword on the caller fixed the issue. Following is the code.

this.Login = function(request) {

    return $http.post(url_login, request)
                        .then(function (result) {
                            return result.data;
                            },
                        function (response) {
                            var data = response.data;
                            var status = response.status;
                            console.log(data + " | " + status);
                            throw response;
                        });

};

On controller

RPCService.Login(req).then(function(data){
       $scope.export.sessionId = data.SessionId;
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.