How to use json api


#1

i want to use a json api to fetch data from json url api, can you suggest me the solution.


#2

Right now i’m create some Factory to wrap my api request, then call it like this

.factory('KreditService', function($http, $localStorage) {
    return {
        cekBaki : function(postData) {
            return $http.post($localStorage.apiUrl + '/kredit/cek-baki', postData, {timeout: $localStorage.connectionTimeout});
        },
        cekTunggakan : function(postData) {
            return $http.post($localStorage.apiUrl + '/kredit/cek-tunggakan', postData, {timeout: $localStorage.connectionTimeout});
        },
        pembayaranAngsuran : function(postData) {
            return $http.post($localStorage.apiUrl + '/kredit/pembayaran-angsuran', postData, {timeout: $localStorage.connectionTimeout});
        }
    }
});

Its then return a promise, then from my controller i call it with

KreditService.cekBaki(data)
            .success(function(res) {
                $ionicLoading.hide();

                $scope.result   = res;

                data.rekening   = data.pin = ""
            })
            .error(function(res) {
                $ionicLoading.hide();

                $ionicPopup.alert({
                    content: res ? res.error.message : $translate('ajax_timeout')
                });
            });

Something like that :smile:


#3

Thank you, let me try.


#4

i want to use this api. http://tmcs.no-ip.info:83/iAutoCount/Profile.svc/json/GetProfile?dbName=5bvq9pq8EokHTf+uB/yLD61bh48juTfB/Up19EK4bks=&uDId=iKNColFa+oztAVriouDfvMpoNDwsYn9H4aJxSVWFIWpqyPnE1k47C5VUkpntIVPr&appsVersion=1.27.5&deviceUsage=180&appRealVersion=1.27.6&osVersion=8.1

how can i do ? to show profile.


#5

You can also use angular-resource, which is a wrapper around $http.

https://docs.angularjs.org/api/ngResource/service/$resource


#6

what you suggest is good to use this api. http://tmcs.no-ip.info:83/iAutoCount/Profile.svc/json/GetProfile?dbName=5bvq9pq8EokHTf+uB/yLD61bh48juTfB/Up19EK4bks=&uDId=iKNColFa+oztAVriouDfvMpoNDwsYn9H4aJxSVWFIWpqyPnE1k47C5VUkpntIVPr&appsVersion=1.27.5&deviceUsage=180&appRealVersion=1.27.6&osVersion=8.1


#7

Can any one give me a good example to use json api . to show profile and many data.


#8

The Angular docs for $http have have examples for get, post and jsonp.

https://docs.angularjs.org/api/ng/service/$http

You could also use an Angular resouce for REST based services

https://docs.angularjs.org/api/ngResource/service/$resource


#9

any sample project in github or can you please give me the sample code where to write and put api and how to call in html.


#10

The angular $http doc has examples and that’s what I used to get up and running.
Plenty of examples on StackOverflow too. Maybe try some code out and if you get stuck post what you’ve got and someone may help.


#11

Thank you , i will try still if you give some examples it will help me, i found lot of things so confused which one i use.


#12

You could try this (adapted from the Angular $http docs that I posted):

var myUrl =  http://tmcs.no-ip.info:83/iAutoCount/Profile.svc/json/GetProfile?dbName=5bvq9pq8EokHTf+uB/yLD61bh48juTfB/Up19EK4bks=&uDId=iKNColFa+oztAVriouDfvMpoNDwsYn9H4aJxSVWFIWpqyPnE1k47C5VUkpntIVPr&appsVersion=1.27.5&deviceUsage=180&appRealVersion=1.27.6&osVersion=8.1;    

 // Simple GET request example :

$http.get(myUrl).
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
       console.log('SUCCESS');
       console.log(data);
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
       console.log('ERROR ' + status);
      });

#13

In Ionic frame work, in controller.js,services.js and app.js in which file what we write and in html how we call this, can you please help me…


#14

Sorry, I have been as helpful as I am able to be at this point. I think the real issue here is a lack of knowledge of Angular. I recommend that you work through some basic beginner Angular tutorials and then read the docs for $http service that I posted. Then you wil find all the Angular examples a lot easier to understand.


#15

okey. but if you just guide in controller and services and app .js how i write and call in ionic it will help me lot…


#16

You can use the $http service in either a controller or a service.
The example at the bottom of the $http docs shows how to fetch data in the controller, assign it to a $scope variable and show it in html.


#17

do you have any app that is exist in github so i can follow.


#18

No, sorry. What is ist about that example you don’t understand?


#19
here is my controller 
.controller('ProfileCtrl', ['$scope', 'Evenimente', function($scope, Evenimente){
        $scope.profile = [];
        Evenimente.getAll().then(function (profile) {
          $scope.profile = profile;
        }, function (response) {
            // our q.reject gets the reponse and you can handle an error
            console.log("Rejected connection for Evenimente");
        });
    }]);

here is my services 
'use strict';

 angular.module('starter.services', [])
.factory('Evenimente', ['$http', '$q', function($http, $q){
  return {
    getAll: function(){
      var myPromise = $q.defer();
      $http.get('http://tmcs.no-ip.info:83/iAutoCount/Profile.svc/json/GetProfile?dbName=5bvq9pq8EokHTf+uB/yLD61bh48juTfB/Up19EK4bks=&uDId=iKNColFa+oztAVriouDfvMpoNDwsYn9H4aJxSVWFIWpqyPnE1k47C5VUkpntIVPr&appsVersion=1.27.5&deviceUsage=180&appRealVersion=1.27.6&osVersion=8.1').then(function(response){
                  myPromise.resolve(response.data.posts); // resolve our promise -> success
               }, myPromise.reject); // reject our promise -> fail / error case
      return myPromise.promise;
    }
  };
}]);

can you please check what is correct format to write this...

#20

Please can you edit your post with formated code? You can use thr </> symbol in the post editor to help. Thanks