简体   繁体   中英

How to get data from external JSON with Ionic?

Currently i'm developing an app using Ionic Framework.

I need to get data from external JSON file using token and parameter (1 for premium or 2 for basic)

The URL display data correctly, but i don't understand how to get data from my "pageCtrl".

I try this http://fdietz.github.io/recipes-with-angular-js/consuming-external-services/requesting-json-data-with-ajax.html

But in the app only see 3 blocks without data

I'm using this code on controller.js

// Controller of docs.
appControllers.controller('docsCtrl', function ($scope, $mdBottomSheet, $mdToast, $mdDialog, $http) {

    $http.get('https://www.domain.com/api/document?__token=[the_token_added_on_code]&__idPortal=1').
    success(function(data, status, headers, config) {
        $scope.docs = data;
    }).
    error(function(data, status, headers, config) {
    // log error
    });

}); // End of docs controller.

On HTML:

<ion-view view-title="Documentos">

    <!--dashboard section-->
    <ion-content id="docs-content">

        <div class="documentos padding">

            <ion-list>
                <ion-item ng-repeat="doc in docs" class="item-avatar">
                    <h2>{{doc.name}}</h2>
                    <p>{{doc.description}}</p>
                    <p>{{doc.created}}</p>
                </ion-item>
            </ion-list>
        </div>

    </ion-content><!--end dashboard section-->
</ion-view>

Finally, the URL return this data:

{"success":true,"return":{"totalItem":"33","totalPages":7,"pageSize":5,"itens":[{"id_document":"4760","name":"Teste","created":"02\/09\/2015 16:57:00","id_type":"108","type":"Documento Teste","description":"Documento"},{"id_document":"4722","name":"Ata de assembleia 08\/2015","created":"31\/08\/2015 17:32:55","id_type":"3","type":"Ata da assembl\u00e9ia","description":null},{"id_document":"4400","name":"Regimento","created":"04\/08\/2015 16:47:30","id_type":"108","type":"Documento Teste","description":"Regimento interno "},{"id_document":"4261","name":"ATA da AGE em 18\/09","created":"26\/07\/2015 22:22:39","id_type":"3","type":"Ata da assembl\u00e9ia","description":null},{"id_document":"2964","name":"Novo regimento playground","created":"05\/05\/2015 14:30:17","id_type":"91","type":"Regimento Interno","description":"Segue novo regimento playground"}]}}

In advance, thank's for your support

If you return your data correctly you have to get the items from attr itens

 $scope.docs = data.itens;

so please log your data console.log(data) to find the properties in the object and navigate to it {status: true, return: { .. itens:[] }}

$scope.docs = data.return.itens

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM