本文介绍了如何将请求发送到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-10How 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?
发布评论