consume all the records of an angular.js rest petition

时间:2016-04-04 16:57:10

标签: javascript angularjs angular-http

I'm consuming a service REST, and it currently has 5025 records, but when I use the service only 1,000 records appear. What I can do to consume absolutely all records?

this is an example of my code:

  $http({
    method: 'GET',
    url: "www.exampleurl.com",  //is a example, I can not post the real url.
    timeout:5000
  }).success(function(data){
    console.log(data)
  }).error(function(response,status,headers,config) {
   console.log("problem")
  });

1 个答案:

答案 0 :(得分:1)

此API的文档是否指出客户端应如何处理分页?通常会提供某种寻呼令牌。假设您可以批量生成1000个请求,您可以采用强制递归方法:

         var dataSet = [];
        function getExampleData(skip){

        return new Promise(function(fulfill,reject){
            return $http.get('http://www.example.com?skip='+skip).then(function(res){
                    return fulfill(res);
            });
        }).then(function(res){
            if(res.data) //<--or whatever will indicate there were results and we know we need to keep going
                {
                     return getExampleData(skip +1000);
                }
                //if there are no results (or whatever our stop criteria is) we return the full dataset

                return dataSet;

        });

    }
//initial call into 
return getExampleData(0).then(function(results){
console.log(results);
});