duangu6588 2015-02-19 02:23
浏览 38
已采纳

Json响应angularjs无法正常工作

I am new to angularjs and for some reason the angularjs does not load any data on the page when json is from joomla component.

Angularjs works when I get the data from getcustomers.php file which is in the root of the site but does not when the json data is coming from a joomla component. (http://localhost/testsite/index.php?option=com_helloworld&view=helloworld&format=raw).

I can see that the json is loading ( Did hit the url directly for both getcustomers.php and joomla component can also see in the chrome developer tools when the page loads).

The only difference in both the json responses, I can see is the double brackets [{"Id":87,"EANHotelID": }](working Json example) while the not working json example is [[{"Id":87,"EANHotelID":}]].

Angularjs controller is below:(the only difference between working and not working is the HTTP.GET below:

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;
    };
});

Hope you can help.

  • 写回答

1条回答 默认 最新

  • dongpu7881 2015-02-19 02:42
    关注

    A couple of things:

    1. The JSON being fetched is invalid. You should assign a value to the "EANHotelID" property such as null if no value is present. It would look like this:

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

    2. The second response that doesn't work is an array with an array inside of it. To access the data like you are doing you would have to change the first line in your $http.get success function from:

    $scope.list = data;

    to

    $scope.list = data[0];

    It would be better to send the response without the extra array wrapper since you don't need it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记