来自api的$ http GET对象数组

时间:2016-05-03 14:09:23

标签: angularjs web-services rest airtable

以下是我的AirTableService.js

(function () {
    "use strict";   
    var AirTableService = function ($http, $q) {
        var AirTableMethods = {
            getMyRounds: function(AirTable_secret){
                var deferObject_myRounds;
                var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&callback=JSON_CALLBACK', {
                    headers : {
                        'Authorization' : AirTable_secret.apikey,
                        'Content-Type' : 'application/json'
                    }
                });
                deferObject_myRounds = deferObject_myRounds || $q.defer();

                myRounds_promise.then(function(data){
                    deferObject_myRounds.resolve(data);
                });                
                return deferObject_myRounds.promise;
            }
        };
        return AirTableMethods;
    };

    AirTableService.$inject = ['$http', '$q'];

    angular.module('appGolf')
      .service('AirTableService', AirTableService);

}());

在那里你可以看到,使用AirTable' api我正在尝试从我的表中GET数据。我传递参数viewmaxRecords并且它有效。 文档说明我可以通过sort

enter image description here

然后我改为,

https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&sort=[{field:'RoundID',direction:'desc'}]&callback=JSON_CALLBACK

显然没有用,它给了我这个错误, enter image description here
我知道这是因为sortarray of objects而且我知道我的传递方式是不正确的。

我的问题是,您如何在AngularJS

中执行此操作

提前谢谢你。

2 个答案:

答案 0 :(得分:2)

找到答案here 如上所述,我需要添加,

paramSerializer: '$httpParamSerializerJQLike',

如果你感兴趣,我的功能现在看起来像,

var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXX/Rounds?callback=JSON_CALLBACK', {
                params: {
                    view: 'Main View',       
                    maxRecords: 10,
                    sort: [{"field": 'RoundID', "direction":'desc'}]                        
                },
                paramSerializer: '$httpParamSerializerJQLike',                    
                headers : {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });

感谢大家的建议并帮助我。

答案 1 :(得分:1)

您的服务非常冗长且难以阅读。我会这样写:

var app = angular.module("myApp", [ /* dependencies */ ]);

app.factory("AirTableService", ["$http", function($http) {
    return {
        getMyRounds: function(AirTable_secret) {
            return $http.get('path/to/API', {
                //put your sorting JSON object here in params
                params: { sort: [{field: "RoundID", direction: "desc"}] },
                headers: {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });
        },
        anotherMethod: function() {
            //etc....
        },
        yetAnotherMethod: function() {
            return $http.post(); //maybe POST something
        }
    };
}]);

将其注入您的控制器并使用:

AirTableService.getMyRounds(airtableSecret).then(successCallback).catch(errorCallback);