【问题标题】:Json response with angularjs not workingangularjs的Json响应不起作用
【发布时间】:2015-02-19 10:42:33
【问题描述】:

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

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

我可以看到 json 正在加载(确实为 getcustomers.php 和 joomla 组件直接点击了 url 也可以在页面加载时在 chrome 开发人员工具中看到)。

我可以看到两个 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;
    };
});

希望你能帮忙。

【问题讨论】:

    标签: javascript php json angularjs joomla


    【解决方案1】:

    有几点:

    1. 获取的 JSON 无效。如果不存在任何值,则应为“EANHotelID”属性分配一个值,例如 null。它看起来像这样:

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

    2. 第二个不起作用的响应是一个数组,其中包含一个数组。要像您一样访问数据,您必须更改 $http.get 成功函数中的第一行:

    $scope.list = 数据;

    $scope.list = 数据[0];

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

    【讨论】:

    • 这确实解决了我的问题....!!!多谢。这实际上是额外的阵列......现在是凌晨 3 点......所以再次感谢,晚安......!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多