for循环不调用REST风格的API

问题描述:

这里是我的代码整洁for循环不调用REST风格的API

,你可以看到我有做一个for循环和传递[I]变量的API 现在是什么happeing是for循环没有触及api并返回到循环。任何解决方案

var a = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12','a13','a14','a15','a16','a17','a18','a19','a20','a21','a22','a23','a24','a25','a26','a27','a28','a29','a30','a31','a32','a33','a34','a35','a36', 'a37'] 

console.log(a) 

for(var i = 0; i <= a.length; i++){ 

      $http({ 
      url: '/MyApp/getIt?urlofpage=https://www.example.com/alpha/'+a[i]+'.html', 
     method: "POST", 
      }).success(function(response){ 

     console.log(a[i]); 
     console.log(response); 
      }); 

} 
+0

这是一个非常糟糕的做法。您应该将**整个列表**发送到服务器,而不是循环中的每个元素。让服务器负责每个元素。 –

+0

我无法发送整个列表...每个URL都不同于解析该特定网址的值@koby – doe

+0

我无法理解为什么它被低估了没有任何解决方案? – doe

因为它是异步的,所以不能使用for循环进行数据库调用。请看下面的代码。

var a = ['a1','a2','a3','a4', ...]; 

    var i=0; 
checkArrayLength(); 
function checkArrayLength(){ 
    if(i<a.length){ 
     makeRequest(a[i]); 
    } 
} 

    function makeRequest(param1){ 

     $.ajax({ 
      url: '/MyApp/getIt?urlofpage=https://www.example.com/alpha/'+param1+'.html', 
      method: "POST", 
      success: function(response) { 
       console.log(a[i]); 
       i=i+1; 
       console.log(response); 
       checkArrayLength(); 
      }, 
      error:function(response) { 
       console.log(a[i]); 
       i=i+1; 
       console.log(response); 
       checkArrayLength(); 
      } 
     }); 

    } 

它看起来像你试图使用jQuery ajax来获取API调用。 如果是这样的话,你应该使用$.post$.ajax

var a = ['a1','a2','a3','a4', ...]; 
console.log(a); 
for(var i = 0; i <= a.length; i++){ 
    $.ajax({ 
    url: '/MyApp/getIt?urlofpage=https://www.example.com/alpha/'+a[i]+'.html', 
    method: "POST", 
    success: function(response) { 
     console.log(a[i]); 
     console.log(response); 
    } 
    }); 
} 

否则,请分享$http功能。

+0

$ Http是角度函数,就像$,jQuey中的AJAX调用AngularJs中的http方法 – doe

+0

我的不好。那么,你能检查一些特定的迭代是否失败,或者所有这些? (可能在调用服务器端代码时调试它)。你可以正确地访问API,但在这方面存在问题,因此永远不会获得成功。你也可以尝试在JS中处理错误响应,看看它是否被解雇。 –

+0

当我通过手动URL:'/MyApp/getIt?urlofpage=example.com/alpha/a1.html'; ...网址:'/MyApp/getIt?urlofpage=example.com/alpha/a2.html' ;,它工作正常 – doe