javascript - Die for-Schleife führt mehrere asynchrone Anforderungen aus, aber die Reihenfolge der angeforderten Daten ist gestört?
ringa_lee
ringa_lee 2017-06-12 09:26:54
0
3
634
$http({
        'url':'/jtkj/yjmc',
        'method':'POST',
    })
    .success(function(res) {
        $scope.yjdh = res;
        console.log(res);
        var navUrl = ['/genSit','/news','/set','/manage','/cooperate','/admNet','/eplNet','/stuMan'];

        //传值,循环遍历出数据
        $scope.xlcd = [];
        for(var i=0;i<res.length;i++){
            $scope.yjid = res[i].yjid;
            console.log(res[i].yjid);
            ejnameGet($scope.yjid);
            $http({
                url:'/jtkj/ejmc',
                method:'POST',
                data:{yjid:$scope.yjid}
            }).success(function(xlcd1){
                //$scope.xlcd = xlcd1;
                console.log(xlcd1);
                xlcd.push(xlcd1);
            })
        }
        //console.log($scope.xlcd);
        $scope.xlcd = xlcd;
        console.log($scope.xlcd);
ringa_lee
ringa_lee

ringa_lee

Antworte allen(3)
世界只因有你

异步请求哪个完成就push哪个,会乱是正常的吧;你别用push,直接通过i给下标,注意i值的处理就行了(可以使用IIFE)

黄舟

有一个思路是构造一个存储结构比如数组array,当异步函数i执行完成后,将数据放回array[i]。

大家讲道理

你可以用promise.all方法啊,把请求放进一个数组,这样顺序就不会乱了啊。

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage