0

I want to make multiple ajax calls from different urls in football-data.org API. Here's my code sample:

angular.module('liveFootball', ['ionic'])
.constant('urls', {
    BASE: 'http://api.football-data.org/',
    BASE_API: 'http://api.football-data.org/v1/soccerseasons/',
    TEAM_URL: 'http://api.football-data.org/v1/teams',
    HEAD: {
        'X-Auth-Token': 'e7486677a2dd4260b7aeb8a464749e80'
    }
});
        getAllFixtures: function(leagueID){
            var getAllFixtures = {
                method: 'GET',
                url: urls.BASE + "fixtures?timeFrame=n14",
                headers: urls.HEAD
            }
            return $http(getAllFixtures);
        },

Is there a way I can include another url in this call? Thanks.

4
  • Your function name getAllFixtures and object name getAllFixtures in the function are the same? Anyway, do you want them all to return at the same time or you don't care about the order? Commented May 8, 2017 at 21:58
  • All I want is the possibility to add another url after the fixtures Commented May 8, 2017 at 22:06
  • It's not possible to have more than one url. each url has to go with a separate request. Commented May 8, 2017 at 22:21
  • It is unclear what you are asking. Do you want to traverse the API using the data returned by the first call? Or do you want to execute multiple API calls in parallel? And what do you want to do with the leagueID parameter? Commented May 8, 2017 at 23:41

2 Answers 2

2

It's not possible to have more than one url field in the $http config object, but you can send the three requests and use Promise.all() $q.all to await their responses. The response will be a promise which when you .then() will have an array containing all the responses.

    getAllFixtures: function(leagueID){
            var sources = [
                 urls.BASE,
                 urls.BASE_API,
                 urls.TEAM_URL
            ];
            var promises = [];
            for(var i=0; i<sources.length; i++){
                promises.push($http({
                    method: 'GET',
                    url: sources[i] + "fixtures?timeFrame=n14",
                    headers: urls.HEAD
                }));
            }
            return  ̶P̶r̶o̶m̶i̶s̶e̶.̶a̶l̶l̶ $q.all(promises);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful when using ES6 promises with the AngularJS framework. ES6 promises can cause problems when their .then blocks modify $scope. It is safer to use AngularJS $q.all.
0

There is no way you can include another url , you have to call it again. You can use $q.all in angular to make multiple request at once. For example:

    var request = [getAllFixtures('10'), getAllFixtures('11)];
     $q.all(request).then(function (value) {

     },function(err){

     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.