如何将请求发送到JSON?

编程入门 行业动态 更新时间:2024-10-27 21:16:41
本文介绍了如何将请求发送到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?

本文发布于:2023-10-11 22:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   如何将   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!