$http 从 api 获取对象数组

$http GET array of objects from an api

下面是我的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);

}());

如您所见,我正在尝试使用 AirTableapi GET 来自我的 table 的数据。我正在传递参数 viewmaxRecords 并且它有效。 文档说明我可以通过 sort,

然后我改成了,

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

很明显这不起作用,它给了我这个错误,
我知道这是因为 sortarray of objects 并且我知道我传递的方式是不正确的。

我的问题是,你是如何在 AngularJS 中做到这一点的?

提前谢谢你。

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

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

找到答案 正如那里提到的,我需要补充,

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

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