ngResource不以json对象格式返回数据

时间:2016-02-01 00:59:02

标签: angularjs json node.js ngresource

我已经设置了一个基本的角度和节点应用程序。节点服务器以JSON格式返回响应:

[{"FeedId":"56aa6aece3d7031b044b34de","Name":"geeksforgeeks","FeedUrl":"http://www.geeksforgeeks.org/feed/?paged=INDEX","FeedItems":[]}]

节点方法:

router.get('/feeds', function(req, res, next) {
    request.get(webApiUrl + '/api/Feed', { headers: {
        'Content-Type': 'application/json'
    }}, function(error, response, body)
    {
        if(error) {
            return next(error);
        }

        res.json(body);
    });
});

在角度方面,我使用ngResource,我已将配置定义为:

this.$resource("http://localhost:3000/feeds/:feedName", {}, 
                {
                transformResponse: function(data, headers) {
                    return angular.fromJson(data);
                },
                post: {method:'POST'},
                get : {method: 'GET'},
                query: {method: 'GET', isArray: false },
                update: {method: 'PUT', isArray: false},
                'delete': {method:'DELETE', params: { feedName:"@feedName" }}
            });

我面临的问题是ngResource没有以json对象格式返回结果,而是以字符数组转换响应。

var resource = this.dataAccessService.getFeedResource();
            this.feeds = [];
            let _me = this;
            resource.get((data) => {
                console.log(data); **//array of characters**
                angular.forEach(data, function(ff) {
                    console.log(ff.Name);**//undefined**
                });
            });

如何使ngResource以json对象格式返回数据,类似于节点服务器返回的数据?

0 个答案:

没有答案