Angularjs:为什么服务查询提取的数据被视为对象,Angular不会使用

时间:2014-05-19 17:43:19

标签: javascript php arrays angularjs angularjs-directive

在我的服务(工厂)中,我进行查询以从我的服务器PHP页面获取数据。 两种情况:

  1. 如果我通过调用函数(返回数组)然后回显json_encode($ data)来提供数组;在底部,Angular会抱怨资源配置错误(类似于数据类型错误,对象而不是数组,反之亦然,我会根据我是否设置isArray为true或false);

  2. 如果我创建一个具有相同结构的数组并注释掉其他代码,只留下echo json_encode($ data);在底部,该死的东西会使用那些数据!

  3. 最令我惊讶的是 - 如果我打开2个选项卡,print_r或var_dump这两个数组类型,它们将完全相同!

    // I call my function to retrieve Google Plus posts like this
    // in my response.php file which is being called for data
    $pluses = $serviceObj->loadActivities($uId);
    
    // That method creates then returns the resulting array
    // note that I have to reach deeper into the response to take out just the 
    // level i'm interested in - that is "items". 
    // I used $key here to make sure each post is numerically indexed
    // and I don't think that is the culprit but who knows..
    
    public function loadActivities($uId)
    {
        $items = array();
        $response = $this->service->activities->listActivities($uId, 'public', array('maxResults'=>4));
        foreach($response['modelData']['items'] as $key => $item){
            $items[$key]['displayName'] = $item['actor']['displayName'];
            $d = new \DateTime($item['published']);
            $items[$key]['publishedDate'] = $d->format("n/j/Y");
            $items[$key]['publishedMonth'] = $d->format("F");
            $items[$key]['imageSrc'] = $item['object']['attachments'][0]['image']['url'];
            $items[$key]['content'] = $item['object']['content'];
        }
    
        return $items;
    }
    
    // Now, this array looks like this when I do print_r on it
    $arr = array(
        0 => array(
            'displayName' => 'thevalue',
            'publishedDate' => 'thevalue',
            'publishedMonth' => 'thevalue',
            'imageSrc' => 'thevalue',
            'content' => 'thevalue'
        ),
        1 => array(
            'displayName' => 'thevalue',
            'publishedDate' => 'thevalue',
            'publishedMonth' => 'thevalue',
            'imageSrc' => 'thevalue',
            'content' => 'thevalue'
        ),
        ....
    );
    
    // at the bottom of the file, there is this
    echo json_encode($arr);
    // so that Angularjs can get the data. This does not work (the error 
    // at the bottom of this question is triggered
    

    我甚至尝试将我的手动类型数组从PHP页面移动到我的函数中,该函数返回上面案例1中的数组 - 然后返回该数组(我这样做是为了测试同一个数组) - 仍然是错误!

    // so basically, if I manually type the same array but directly in 
    // the response.php file, that is, the array is no longer returned by the function,
    // and then
    echo json_encode($arr);
    // at the bottom, Angularjs stops complaining and consumes it.
    

    我的服务

    angular.module('socialPosts.services', ['ngResource'])
        .factory('PostsResource', ['$resource', function($resource){
                return $resource('response.php', {}, {
                    query: { method: 'GET', isArray: true }
                })
        }])
    ;
    

    我的控制器

    angular.module('socialPosts.controllers', [])
        .controller('LoadPostsCtrl', ['$scope', 'PostsResource', function($scope, PostsResource){
                $scope.init = function(network, ename){
                    $scope.entry = ename;
                    $scope.loadTabContent(network);
                };
                $scope.content = [];
                $scope.loadTabContent = function(called){
                    $scope.networkCalled = called;
                    $scope.content = PostsResource.query( {from: $scope.networkCalled, company: $scope.entry } ).$promise.then(
                            function(result){
                                    $scope.content = result;
                            },
                            function(error){
                                alert(error);
                            }
                        );
                };
        }]);
    

    我别无选择,只能假设这与数据是通过函数调用获得的事实有关,因为无论如何我从服务器(PHP页面)获取数据。

    实际错误:

      

    错误:$ resource:badcfg   响应与配置的参数不匹配   资源配置出错。预期的响应包含一个数组但得到一个对象   描述   当$ resource服务期望响应可以作为数组反序列化,接收对象,反之亦然时,会发生此错误。默认情况下,所有资源操作都需要对象,但需要数组的查询除外。

         

    要解决此错误,请确保您的$ resource配置与从服务器返回的数据的实际格式相匹配。

    有关更多信息,请参阅$ resource API参考文档。

1 个答案:

答案 0 :(得分:0)

看起来很奇怪,事实证明应该帮助的东西实际上是罪魁祸首。评论出行后

error_reporting(E_ALL);

函数返回的结果数组工作!