Javascript Angularjs:为什么服务查询获取的数据被视为对象并赢得';Don’不要被人用

Javascript Angularjs:为什么服务查询获取的数据被视为对象并赢得';Don’不要被人用,javascript,php,arrays,angularjs,angularjs-directive,Javascript,Php,Arrays,Angularjs,Angularjs Directive,在我的服务(工厂)中,我执行一个查询以从服务器PHP页面获取数据。 两种情况: 如果我通过调用函数(返回数组)提供数组,然后返回json_encode($data);在底部,Angular会抱怨资源配置错误(比如数据类型错误,对象而不是数组,或者相反,我会根据是否将isArray设置为true或false来判断) 如果我创建一个相同结构的数组并注释掉其他代码,只留下echo json_encode($data);在底部,该死的东西会使用这些数据 最让我吃惊的是,如果我打开两个选项卡,打印或变量转

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

  • 如果我通过调用函数(返回数组)提供数组,然后返回json_encode($data);在底部,Angular会抱怨资源配置错误(比如数据类型错误,对象而不是数组,或者相反,我会根据是否将isArray设置为true或false来判断)

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

  • 最让我吃惊的是,如果我打开两个选项卡,打印或变量转储这两种数组类型,它们将完全相同

    // 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参考文档。

    尽管看起来很奇怪,但事实证明,本应提供帮助的东西实际上是罪魁祸首。评论完后,我就离开了

    error_reporting(E_ALL);
    

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

    这是PHP中的一个常见错误,与PHP可能根据其键的类型和值选择将对象解释为关联数组(object)或索引数组(array)有关。您的
    $resource
    请求需要一个JSON编码的数组或JSON编码的对象,但PHP没有以该格式返回它(即,它选择将“thing”编码为关联数组而不是索引数组(这会导致JSON编码的对象而不是JSON编码的数组),反之亦然)。显示一些代码可能会导致确切的问题和解决方案。@ExpertSystem好的,我刚刚添加了一些代码块,这些代码可能会对正在发生的事情有所帮助。