0

I get some data from a WebApi, the answer (below the code to get the datas) is in JSON. But I can't access this result from angularJS. The datas look like :

{
  "$id": "1",
  "result": [
    {
      "$id": "2",
      "name": "Français",
      "code": "FR",
      "id": 1
    },
    {
      "$id": "3",
      "name": "Néerlandais",
      "code": "NL",
      "id": 2
    },
    {
      "$id": "4",
      "name": "English",
      "code": "EN",
      "id": 3
    }
  ]
}

But I get the error below when I try to display the result :

data.result is undefined

I get the data like this :

(function () {
    angular.module('myApp')
        .factory('dataService', ['$q', '$http', dataService]);

    function dataService($q, $http) {
        return {
            initFormCustomer: initFormCustomer
        };

        function initFormCustomer() {
            return $http({
                method: 'GET',
                url: 'http://localhost:123456/api/forminit/customer/',
                headers: {
                },
                transformResponse: transformCustomer,
                cache: true
            })
            .then(sendResponseData)
            .catch(sendGetCustomerError)
        }

        function sendResponseData(response) {
            return response.data;
        }

        function transformCustomer(data, headersGetter) {
            var transformed = angular.fromJson(data.result);
            console.log(data.result[0]);
            return transformed;
        }

        function sendGetCustomerError(response) {
            return $q.reject('Error retrieving customer(s). (HTTP status: ' + response.status + ')');
        }
    }
}());

The controller :

(function () {

    angular.module('myApp')
        .controller('customerController', ['$location', '$scope', 'dataService', CustomerController]);

    function CustomerController($location, $scope, dataService) {
        var vm = this;

        vm.languages = dataService.initFormCustomer();
    }

}());
1
  • console.log(data). somewhere between receiving the "answer" and the done callback you're unwrapping it. Commented May 31, 2015 at 15:12

1 Answer 1

1

I think the transform function gets a json string that you have to deserialize before using it as an object... try sth like:

function transformCustomer(data, headersGetter) {
        var transformed = angular.fromJson(data);
        console.log(transformed.result[0]);
        return transformed.result;
    }

Additionally you may look at the docs https://docs.angularjs.org/api/ng/service/$http . There is some code showing how to append a transform to the default one (that do the deserialization and XSRF checks)

Sign up to request clarification or add additional context in comments.

2 Comments

In the controller when I do this "var languages = dataService.initFormCustomer(); alert(languages[0]);" I get an undifined and in the console "Error: [$rootScope:inprog] $digest already in progress". When I try this "alert(languages.result[0]);" I get "result is undifined". But in the transformCustomer that's look ok
You are returning a promise in initFormCustomer - so you can't use it directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.