AngularJS SyntaxError:位于0的JSON中的意外标记

时间:2017-02-20 18:38:09

标签: php angularjs json codeigniter

我正在尝试使用angularJS + codeigniter进行分页,我有一个名为“Cursos”的控制器和一个名为“api”的函数来获取数据

函数api代码:

{"DadosDosCursos":[{"i_imc_ID":"2","i_imc_cargaHoraria":"40 horas","i_imc_categoriaIDS":"1,2","i_imc_dataCreated":"2017-02-20 11:36:36","i_imc_etapasAprendizagem":"Etapa 1\r\netapa 2","i_imc_imagemLogo":"web-design-criacao-de-sites_04042016162740.png","i_imc_imagemMiniatura":"imagepress-impressos-profissionais_05012016115837.jpg","i_imc_metaDescriptions":"meta description","i_imc_metaTags":"meta tags","i_imc_nome":"Nome do curso 2","i_imc_softwaresIDS":"1","i_imc_status":"1","i_imc_subtitulo":"Subtitulo aqui","i_imc_textoApresentacao":"Apresenta\u00e7\u00e3o"},{"i_imc_ID":"1","i_imc_cargaHoraria":"80 horas","i_imc_categoriaIDS":null,"i_imc_dataCreated":"2017-02-17 00:00:00","i_imc_etapasAprendizagem":"Etapa 1","i_imc_imagemLogo":"modelagem-com-3ds-max--v-ray--photoshop_04042016163311.png","i_imc_imagemMiniatura":"imageweb-criacao-de-sites_14122015100956.jpg","i_imc_metaDescriptions":"meta description","i_imc_metaTags":"meta tags","i_imc_nome":"Curso de desenvolvimento de sistemas","i_imc_softwaresIDS":"1","i_imc_status":"0","i_imc_subtitulo":"subtitulo","i_imc_textoApresentacao":null}],"totalCount":2}

,结果看起来不错:

app.controller('activityTableCtrl', function($scope, $http) {

    $scope.currentPage = 1;
    $scope.totalItems = 0;
    $scope.pageSize = 10;
    $scope.searchText = '';
    getData();

    function getData() {
    $http.get('http://localhost/estudo/site/cursos/api/' + $scope.currentPage + '/' + $scope.pageSize
    //$http.get("../assets/api/database.json"
        )
        .then(function(data) {




            $scope.activity = [];
            $scope.totalItems = data.totalCount;
            $scope.startItem = ($scope.currentPage - 1) * $scope.pageSize + 1;
            $scope.endItem = $scope.currentPage * $scope.pageSize;

            if ($scope.endItem > $scope.totalCount) {
                $scope.endItem = $scope.totalCount;
            }

            angular.forEach(data.DadosDosCursos, function(temp){
                $scope.activity.push(temp);
            });

        });
    }

    $scope.pageChanged = function() {
        getData();
    }
    $scope.pageSizeChanged = function() {
        $scope.currentPage = 1;
        getData();
    }
    $scope.searchTextChanged = function() {
        $scope.currentPage = 1;
        getData();
    }
 })

view source code

JSON formated

我的angularJS控制器:

SyntaxError: Unexpected token  in JSON at position 0

我在console.log {{1}}

上收到此错误

我尝试使用JSON从另一个网址加载,一切都做得很好

1 个答案:

答案 0 :(得分:1)

这可能会也可能不会回答你的问题,但如果你刚刚更换了这个循环会更好:

angular.forEach(data.DadosDosCursos, function(temp){
                $scope.activity.push(temp);
            });

使用:

$scope.activity = data.DadosDosCursos

因为您在那时已经清空了$scope.activity数组,并且您没有修改data.DadosDosCursos中的任何条目结构;)