0

I'm coding a mobile application with ionic. I have to get a data (daily changing data) from a web page with JSON, but I want to get old data too. For example:

data.json?date=2016-11-10
data.json?data=2016-12-10

How can I send request to JSON?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
yucel
  • 365
  • 4
  • 11
  • You want to send a JSON by method GET? – sioesi Oct 14 '16 at 17:41
  • Yes, i want to send date data and i want to get content of that day by json. – yucel Oct 14 '16 at 17:44
  • But your question is how to get the answer to that call ?? – sioesi Oct 14 '16 at 17:45
  • for example, i ve a database, with fields, id and name, now in my mobile application, i ve a list with id's. If you click id 1, you will get the name with id = 1. i m using names.json to get the data, but names file must get the id from the client, so it must be something like that; names.json?id=1 and you will get the name of row with id=1. – yucel Oct 14 '16 at 17:51

3 Answers3

0

To send data from PHP, once you get your data from the database, the array will apply json_encode($array); and to return you put return json_encode ($ array);

Try this!

var date = '2016-11-10';
$http({
       method: 'GET',
       url: data.php,
       params: {date: date},
       dataType: "json",
       contentType: "application/json"

 }).then(function(response) {

 });
sioesi
  • 497
  • 1
  • 5
  • 20
  • can you give me an example of data.json code, because i ve to use php to connect database, how can i do it in json file? – yucel Oct 14 '16 at 17:52
  • Edit my answer! – sioesi Oct 14 '16 at 17:54
  • Of course, you should do such a file on your server or on your localhost: Usuarios.php and that file returns all of the database but in JSON. – sioesi Oct 14 '16 at 17:56
0

The question is confusing, so I'm not sure how to answer. If you are having trouble formatting a request to a REST service, you will need to find out how the service expects the date to be formatted in your field-value pair i.e:

date=2016/11/10 or date=20161110 

If those don't work, this answer may help The "right" JSON date format
However, if you are actually wondering how to serialize a date in JSON, this link may help http://www.newtonsoft.com/json/help/html/datesinjson.htm

Community
  • 1
  • 1
D0ubleGunz
  • 60
  • 7
0

I prefer to use services for ajax requests.

Create a Service

//Service
(function() {
    'use strict';

    angular
        .module('appName')
        .factory('appAjaxSvc', appAjaxSvc);

    appAjaxSvc.$inject = ['$http', '$log', '$q'];

    /* @ngInject */
    function appAjaxSvc($http, $log, $q) {

        return {
            getData:function (date){

              //Create a promise using promise library
                var deferred = $q.defer();

                $http({
                    method: 'GET', 
                    url:'/url?date='+date
                }).
                success(function(data, status, headers,config){
                    deferred.resolve(data);
                }).
                error(function(data, status, headers,config){
                    deferred.reject(status);
                });

                return deferred.promise;
            },
        };
    }
})();

Then Use it in Controller

(function() {

    angular
        .module('appName')
        .controller('appCtrl', appCtrl);

    appCtrl.$inject = ['$scope', '$stateParams', 'appAjaxSvc'];

    /* @ngInject */
    function appCtrl($scope, $stateParams, appAjaxSvc) {
        var vm = this;
        vm.title = 'appCtrl';

        activate();

        ////////////////

        function activate() {

            appAjaxSvc.getData(date).then(function(response) {
                //do something
            }, function(error) {
                alert(error)
            });

        }
    }
})();
Ajeet Lakhani
  • 3,768
  • 2
  • 22
  • 37