0

I'm trying to pull a user's first name after logging in, I'm having problem retrieving the object containing the name.

my service.js:

(function () {
'use strict';
angular
    .module('agent')
    .factory('viewAgent', viewAgent);

viewAgent.$inject = ['$http', '$log'];
function viewAgent($http, $log) {
    var param = {
        api_id: '1',
        api_key: 'KEY',
        api_secret: 'SECRET',
        user_email: 'test-email',
        user_password: 'test'
    };
    return {
        view: function () {
            return $http({
                url: 'api/agent/loginViaEmail',
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
                    $log.log(str);
                    return str.join('&');
                },
                data: param
            });
        }
    };
}

})();

the controller.js:

(function () {
'use strict';

angular
    .module('agent')
    .controller('AgentController', AgentController);

AgentController.$inject = ['viewAgent', '$state', '$log'];
function AgentController(viewAgent, $state, $log) {
    var vm = this;
    vm.agents = [];

    viewAgent.view()
        .then(function success(res) {
            $log.log(res.data);
            vm.agents = res.data.basicInfoAgent;
            return res.data;
        },
        function error(error) {
            $log.log(error);
        });
}
})();

login controller:

function loginClick() {
        viewAgent.view();
    }

and my html:

<div ng-controller="AgentController">
  <md-button ng-click="vm.loginClick()" ng-disabled="login.$invalid">Login</md-button>
  <div>{{vm.agents}}</div>
</div>

I'm getting a 400: Bad Request error that says, "There are missing parameter field".I'm not sure what to fix. I've tried retrieving the data on Postman and I was able to get back the whole user's object.

Audi AE
  • 3
  • 2
  • Are you sure it's `application/x-www-form-urlencoded`? Maybe it expects application/json. Check headers in both cases. – dfsq Jul 23 '16 at 18:32
  • 1
    You need to post the server side code that is causing the 400 error. – Robert Moskal Jul 23 '16 at 18:49
  • I am not sure, but try to do data: `$.param({api_id: "1"})` and add your data to that and pass it to $http, See http://stackoverflow.com/a/11443066/3176270 – Mohammad Kermani Jul 23 '16 at 21:14

0 Answers0