以批量形式调用angularjs中的HTTP请求?

use*_*346 10 ajax http angularjs

我有两个for循环和一个HTTP调用.

for(i=0;i<m;i++) {
  for(j=0;j<n;j++) {
    $http call that uses i and j as GET parameters
    .success(//something)
    .error(//something more)
  }
}
Run Code Online (Sandbox Code Playgroud)

这个问题是它根据m和n的值进行大约200-250次AJAX调用.当尝试从移动设备访问时,这会导致浏览器崩溃问题.我想知道是否有办法以批量形式调用HTTP请求(一次n个请求),一旦这些调用完成,移动到下一批等等.

Jon*_*Jon 7

您可以始终使用正确的HTTP批处理模块,例如angular-http-batcher - 它将获取所有请求并将它们转换为单个HTTP POST请求,然后再将其发送到服务器.因此它将250个呼叫减少为1个!该模块在这里https://github.com/jonsamwell/angular-http-batcher,详细解释如下http://jonsamwell.com/batching-http-requests-in-angular/


use*_*346 3

我的做法如下(当你想批量调用HTTP请求时,这会有所帮助n requests at a time)

call batchedHTTP(with i=0);

batchedHTTP = function() {
  /* check for terminating condition (in this case, i=m) */
  for(j=0;j<n;j++) {
    var promise = $http call with i and j GET parameters
    .success(// do something)
    .error(// do something else)

    promisesArray.push(promise);
  }
  $q.all(promisesArray).then(function() {
    call batchedHTTP(with i=i+1)
  });
}
Run Code Online (Sandbox Code Playgroud)