【问题标题】:AngularJS make http post synchronous order with thenAngularJS 使 http post 同步顺序与 then
【发布时间】:2021-07-24 15:44:07
【问题描述】:

我有一个返回 json 的文件的 http 帖子,然后我使用 .then(function(response){}.... 但是我如何无法获得另一个连续发生的 http 帖子,然后继续第二个。然后? 我想要类似的东西

        $http({
        method:"POST",
        url:"....",
        data:{'....}).then(function(response){...})

        $http({
        method:"POST",
        url:"....",
        data:{'....}).then(function(response){...})

但是要依赖于它的另一个而不是像下面那样发生异步我想要一些东西

        $http({
        method:"POST",
        url:"....",
        data:{'....})First_then().then(function(response){...})

更新

我想明天我会为将来遇到相同问题的每个人发布解决方案,这实际上很简单!

【问题讨论】:

    标签: angularjs promise


    【解决方案1】:

    你想从第一个调用第二个$http

    类似:

    function secondRequest(dataFromFirst) {
      const data = {
        someProp: dataFromFirst.someprop
      }
      return $http({
        method: "POST",
        url: "....",
        data: data
      }).then(function(response) {
        // return some combination of new response and dataFromFirst
      })
    }
    
    $http({
        method: "POST",
        url: "....",
        data: {}
      })
      .then(secondRequest)
      .then(function(combinedData) {
         // combined data from secondRequest then
      })
    

    【讨论】:

      【解决方案2】:

      问题 需要两个相互依赖的请求作为第二个的输入。 它必须按顺序完成,以便在返回正确的 json 文件后主体可以正确执行

      解决方案 我不得不使用 Promise q.all 首先作为Controller注入区域中的参数,如

                 bookingUpdateView.controller('liveBookingController',['$scope', 
                 '$http', '$q', function($scope, $http, $q){
                  .... code here of entire controller....
                  }]);
      

      然后在控制器内部,我将 2 个承诺作为 $scope 函数并返回,或者我可以简单地将它们原始放入 $q.all([]).then 但这非常混乱,所以

               $scope.promise = function(i){
                  return  $http({
                          method:"POST",
                          url:"model/fetch_data_booking_view_seats_available.php",
                          data:{'movie_name':$scope.movieTitle, 
                          'theatre_n':$scope.list_TP_5[i].theatre_name_fk, 
                          ..... etc.....}
                          })
               } 
      

      接下来,队列本身我有一个主体可以在另一个 $scope 函数中执行,例如

       $scope.updateTimePlay = function (id){
                    //..... code here .......
                 $q.all([$scope.promise(i), 
                 $scope.promise1(i)])
                 .then(function(responses){ 
                      //....body here......
                      //in order to get JSON properly had to do
                      $scope.SeatsSub = responses.map((resp) => resp.data); 
                      //so i could call it like this 
                      $scope.SeatsSub[1][0].seatsAvailable;
                     //or use output to see what going on
                     console.log($scope.SeatsSub)
                  })
                        //....code here .....
               }
      

      【讨论】:

        猜你喜欢
        • 2015-03-01
        • 1970-01-01
        • 2015-02-02
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        • 2014-03-30
        相关资源
        最近更新 更多