如何通过ajax将正确的对象发送到for循环中

如何通过ajax将正确的对象发送到for循环中,ajax,for-loop,Ajax,For Loop,问题是FOR在ajax调用从URL检索数据时继续进行,因此只有数组中的最后一个元素获取数据。 在这种情况下,如何将for与ajax调用同步 for(ii in scope.selMovies){ for(jj in scope.selMovies[ii]){ var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, ""); var yearMovie = sc

问题是FOR在ajax调用从URL检索数据时继续进行,因此只有数组中的最后一个元素获取数据。 在这种情况下,如何将for与ajax调用同步

   for(ii in scope.selMovies){
        for(jj in scope.selMovies[ii]){

            var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, "");
            var yearMovie = scope.selMovies[ii][jj].title.match(/\(.*(20|19)[\d]{2}/)[0].replace(/[^\d]/g, "");

            http.jsonp(url + "&query=" + title + callb).
                success(function (data) { console.log( ii, jj, title, data.results );
                    for (k in data.results){
                        if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
                            scope.selMovies[ii][jj].infoes = data.results[k];
                            break;
                        }
                    }

                }
            );

        }
    }

AngularJS中的承诺是异步的,因此在处理集群AJAX请求时,最好在回答所有请求后使用它来处理所有响应。

AngularJS中的承诺是异步的,因此在处理集群AJAX请求时,最好在回答所有请求后使用它来处理所有响应。

中的承诺AngularJS是异步的,因此在处理集群AJAX请求时,最好在回答所有请求后使用它来处理所有响应。

AngularJS中的承诺是异步的,因此在处理集群AJAX请求时,最好在回答所有请求后使用它来处理所有响应。

您的
console.log(ii,jj,title,data.results)
始终显示最后一个
ii
jj
title

问题不在于for循环在Ajax调用检索数据时继续进行。问题在于
http.jsonp
.success
回调函数是循环中的一个闭包,因此它的行为不像人们天真地期望的那样

阅读以更好地理解正在发生的事情

要修复特定情况,请执行以下操作:

function MakeSuccessFn(scope, ii, jj, yearMovie, title){
   return function (data) { 
     console.log( ii, jj, title, data.results );
        for (k in data.results){
          if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
            scope.selMovies[ii][jj].infoes = data.results[k];
            break;
          }
        }

   };
}
然后在循环中,将
http.jsonp
更改为以下内容

http.jsonp(url + "&query=" + title + callb)
.success(MakeSuccessFn(scope, ii, jj, yearMovie, title));

您的
console.log(ii,jj,title,data.results)
是否始终显示最后的
ii
jj
title

问题不在于for循环在Ajax调用检索数据时继续进行。问题在于
http.jsonp
.success
回调函数是循环中的一个闭包,因此它的行为不像人们天真地期望的那样

阅读以更好地理解正在发生的事情

要修复特定情况,请执行以下操作:

function MakeSuccessFn(scope, ii, jj, yearMovie, title){
   return function (data) { 
     console.log( ii, jj, title, data.results );
        for (k in data.results){
          if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
            scope.selMovies[ii][jj].infoes = data.results[k];
            break;
          }
        }

   };
}
然后在循环中,将
http.jsonp
更改为以下内容

http.jsonp(url + "&query=" + title + callb)
.success(MakeSuccessFn(scope, ii, jj, yearMovie, title));

您的
console.log(ii,jj,title,data.results)
是否始终显示最后的
ii
jj
title

问题不在于for循环在Ajax调用检索数据时继续进行。问题在于
http.jsonp
.success
回调函数是循环中的一个闭包,因此它的行为不像人们天真地期望的那样

阅读以更好地理解正在发生的事情

要修复特定情况,请执行以下操作:

function MakeSuccessFn(scope, ii, jj, yearMovie, title){
   return function (data) { 
     console.log( ii, jj, title, data.results );
        for (k in data.results){
          if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
            scope.selMovies[ii][jj].infoes = data.results[k];
            break;
          }
        }

   };
}
然后在循环中,将
http.jsonp
更改为以下内容

http.jsonp(url + "&query=" + title + callb)
.success(MakeSuccessFn(scope, ii, jj, yearMovie, title));

您的
console.log(ii,jj,title,data.results)
是否始终显示最后的
ii
jj
title

问题不在于for循环在Ajax调用检索数据时继续进行。问题在于
http.jsonp
.success
回调函数是循环中的一个闭包,因此它的行为不像人们天真地期望的那样

阅读以更好地理解正在发生的事情

要修复特定情况,请执行以下操作:

function MakeSuccessFn(scope, ii, jj, yearMovie, title){
   return function (data) { 
     console.log( ii, jj, title, data.results );
        for (k in data.results){
          if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
            scope.selMovies[ii][jj].infoes = data.results[k];
            break;
          }
        }

   };
}
然后在循环中,将
http.jsonp
更改为以下内容

http.jsonp(url + "&query=" + title + callb)
.success(MakeSuccessFn(scope, ii, jj, yearMovie, title));

您需要围绕ajax调用创建一个闭包,以封装在进行ajax调用时随着每次循环迭代而更改的值。请尝试以下操作:

for(ii in scope.selMovies){
    for(jj in scope.selMovies[ii]){
        var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, "");
        var yearMovie = scope.selMovies[ii][jj].title.match(/\(.*(20|19)[\d]{2}/)[0].replace(/[^\d]/g, "");
        doAjax(ii, jj, title, yearMovie);
         }
}

function doAjax(count1, count2, title, yearMovie) {
    http.jsonp(url + "&query=" + title + callb).
        success(function (data) { console.log( count1, count2, title, data.results );
            for (k in data.results){
                if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
                    scope.selMovies[count1][count2].infoes = data.results[k];
                    break;
                }
            }
        }
}

您需要围绕ajax调用创建一个闭包,以封装在进行ajax调用时随着每次循环迭代而更改的值。请尝试以下操作:

for(ii in scope.selMovies){
    for(jj in scope.selMovies[ii]){
        var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, "");
        var yearMovie = scope.selMovies[ii][jj].title.match(/\(.*(20|19)[\d]{2}/)[0].replace(/[^\d]/g, "");
        doAjax(ii, jj, title, yearMovie);
         }
}

function doAjax(count1, count2, title, yearMovie) {
    http.jsonp(url + "&query=" + title + callb).
        success(function (data) { console.log( count1, count2, title, data.results );
            for (k in data.results){
                if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
                    scope.selMovies[count1][count2].infoes = data.results[k];
                    break;
                }
            }
        }
}

您需要围绕ajax调用创建一个闭包,以封装在进行ajax调用时随着每次循环迭代而更改的值。请尝试以下操作:

for(ii in scope.selMovies){
    for(jj in scope.selMovies[ii]){
        var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, "");
        var yearMovie = scope.selMovies[ii][jj].title.match(/\(.*(20|19)[\d]{2}/)[0].replace(/[^\d]/g, "");
        doAjax(ii, jj, title, yearMovie);
         }
}

function doAjax(count1, count2, title, yearMovie) {
    http.jsonp(url + "&query=" + title + callb).
        success(function (data) { console.log( count1, count2, title, data.results );
            for (k in data.results){
                if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
                    scope.selMovies[count1][count2].infoes = data.results[k];
                    break;
                }
            }
        }
}

您需要围绕ajax调用创建一个闭包,以封装在进行ajax调用时随着每次循环迭代而更改的值。请尝试以下操作:

for(ii in scope.selMovies){
    for(jj in scope.selMovies[ii]){
        var title = scope.selMovies[ii][jj].title.replace(/\s*\(.*/, "");
        var yearMovie = scope.selMovies[ii][jj].title.match(/\(.*(20|19)[\d]{2}/)[0].replace(/[^\d]/g, "");
        doAjax(ii, jj, title, yearMovie);
         }
}

function doAjax(count1, count2, title, yearMovie) {
    http.jsonp(url + "&query=" + title + callb).
        success(function (data) { console.log( count1, count2, title, data.results );
            for (k in data.results){
                if(data.results[k].release_date.substr(0, 4) == yearMovie ) {
                    scope.selMovies[count1][count2].infoes = data.results[k];
                    break;
                }
            }
        }
}

你能给我举个例子吗?请。你能给我举个例子吗?请。你能给我举个例子吗?请。你能给我举个例子吗?请。