Json 响应 angularjs 无效

Json response with angularjs not working

我是 angularjs 的新手,由于某些原因,当 json 来自 joomla 组件时,angularjs 不会在页面上加载任何数据。

Angularjs 当我从站点根目录中的 getcustomers.php 文件获取数据时有效,但当 json 数据来自 joomla 组件时则无效。 (http://localhost/testsite/index.php?option=com_helloworld&view=helloworld&format=raw).

我可以看到 json 正在加载(当页面加载)。

两个 json 响应的唯一区别是双括号 [{"Id":87,"EANHotelID": }](工作 Json 示例),而不工作 json 示例是[[{"Id":87,"EANHotelID":}]]

Angularjs控制器如下:(工作和不工作的唯一区别是下面的HTTP.GET:

var app = angular.module('myApp', ['ui.bootstrap']);

app.filter('startFrom', function() {
    return function(input, start) {
        if(input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('customersCrtl', function ($scope, $http, $timeout) {
    $http.get('testsite/index.php?option=com_helloworld&view=helloworld&format=raw').success(function(data){
        $scope.list = data;
        $scope.currentPage = 1; //current page
        $scope.entryLimit = 10; //max no of items to display in a page
        $scope.filteredItems = $scope.list.length; //Initially for no filter  
        $scope.totalItems = $scope.list.length;
    });
    $scope.setPage = function(pageNo) {
        $scope.currentPage = pageNo;
    };
    $scope.filter = function() {
        $timeout(function() { 
            $scope.filteredItems = $scope.filtered.length;
        }, 10);
    };
    $scope.sort_by = function(predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };
});

希望能帮到你。

几件事:

  1. 正在获取的 JSON 无效。如果没有值,您应该为 "EANHotelID" 属性 分配一个值,例如 null。它看起来像这样:

    [{"Id":87,"EANHotelID": 空}]

  2. 第二个不起作用的响应是一个数组,里面有一个数组。要像您一样访问数据,您必须将 $http.get 成功函数中的第一行从:

  3. 更改为

$scope.list = 数据;

$scope.list = 数据[0];

最好在没有额外数组包装器的情况下发送响应,因为您不需要它。