如何将请求发送到JSON? [英] How can I send requests to JSON?

查看:179
本文介绍了如何将请求发送到JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用离子型编码移动应用程序。我必须使用JSON从网页获取数据(每日更改数据),但我也想获取旧数据。例如:

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

如何向JSON发送请求?

解决方案

我更喜欢为ajax请求使用服务。



创建服务

  //服务
(function(){
'use strict';

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

appAjaxSvc。$ inject = ['$ http','$ log','$ q' ;

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

return {
getData:function日期){

//使用promise库创建承诺
var deferred = $ q.defer();

$ http({
方法:'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;
},
};
}
})();

然后在控制器中使用它

 (function(){

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

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

/ * @ngInject * /
函数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)
});

}
}
})();


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?

解决方案

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)
            });

        }
    }
})();

这篇关于如何将请求发送到JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆