如何向JSON发送请求?

时间:2016-10-14 17:35:02

标签: json dynamic ionic-framework

我用离子编码移动应用程序。我必须从带有JSON的网页获取数据(每日更改数据),但我也希望获得旧数据。例如:

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

如何向JSON发送请求?

3 个答案:

答案 0 :(得分:0)

要从PHP发送数据,一旦从数据库获取数据,该数组将应用json_encode($array);并返回您放置return json_encode ($ array);

试试这个!

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

 }).then(function(response) {

 });

答案 1 :(得分:0)

问题很混乱,所以我不确定如何回答。如果您在将请求格式化为REST服务时遇到问题,则需要了解服务如何在字段 - 值对中格式化日期,即:

date=2016/11/10 or date=20161110 

如果这些不起作用,这个答案可能会有所帮助The "right" JSON date format 但是,如果您真的想知道如何在JSON中序列化日期,则此链接可能会有所帮助http://www.newtonsoft.com/json/help/html/datesinjson.htm

答案 2 :(得分:0)

我更喜欢将服务用于ajax请求。

创建服务

//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;
            },
        };
    }
})();

然后在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)
            });

        }
    }
})();
相关问题