如何在Angularjs中"pass variable from $http success to another $http request"?

How to "pass variable from $http success to another $http request" in Angularjs?

我无法从第一个 http get 请求访问输出变量,我需要此数据用于另一个 http Post 请求。

None.

$scope.submit = function(x) {

  $http({
    method: "GET",
    url: url + 'getOSchild',
    params: { ncard: x }
  }).then(function success(response) {
    $scope.osChild = response.data;
    console.log($scope.osChild) // this has an output
  }, function error(response, status) {
    console.log(response)
    console.log(status)
  });


  $http({
    method: "POST",
    url: url + 'printOS',
    data: JSON.stringify({
      CARD_NAME: data_cname,
      C_DATE: data_date,
      C_NUMATCARD: data_ncard,
      C_DISTMEANS: data_means,
      C_TIME: data_time,
      cData: $scope.osChild //this is null
    }),
    header: {
      'Content-Type': 'application/json'
    },
  }).then(function success(response) {
    console.log(response)
  }, function error(response, status) {});

}

我需要 $scope.osChild 出现在我的 http post 请求中。

第一个 GET 调用是异步的,因此 $scope.osChild 最初设置 null。所以建议使用 Promises https://ng2.codecraft.tv/es6-typescript/promises/

$scope.getOSChild = function() {
  var deferred = $q.defer();
  $http.get(url + 'getOSchild')
    .then(function onSuccess(response) {
      $scope.osChild = response.data;
      deferred.resolve(response.data);
  }).catch(function onError(response) {
      console.log(response.data);
      console.log(response.status);
      deferred.reject(response.status);
    });
  return deferred.promise;
};

$scope.submit = function(x) {

  $scope.getOSChild().then(function (osChild) {
    $http({
      method: "POST",
      url: url + 'printOS',
      data: JSON.stringify({
        CARD_NAME: data_cname,
        C_DATE: data_date,
        C_NUMATCARD: data_ncard,
        C_DISTMEANS: data_means,
        C_TIME: data_time,
        cData: osChild
      }),
      header: {
        'Content-Type': 'application/json'
      },
    }).then(function onSuccess(response) {
      console.log(response);
    }, function onError(response, status) {});

  });

};

简单地链接两个 XHR:

function getOSChild (x) {
    return $http({
        method: "GET",
        url: url+'getOSchild',
        params: {ncard: x}
    }).then(function success(response) {
        $scope.osChild = response.data;
        console.log($scope.osChild); // this has an output
        return response.data;
     },function error(response) {
        console.log(response)
        console.log(response.status);
        throw response;
    });
}

$scope.submit = function(x) {  
    getOSChild(x).then(function(osChild) {
        $http({
            method: "POST",
            url: url+'printOS',
            data:{ CARD_NAME: data_cname, 
                      C_DATE: data_date,
                 C_NUMATCARD: data_ncard, 
                 C_DISTMEANS: data_means,
                      C_TIME: data_time, 
                       cData: osChild //chained

            }
        }).then(function success(response) {
              console.log(response)
        });
    });
};

.then 方法 return 是一个 新承诺 ,通过 successCallback 的 return 值解决或拒绝, errorCallback(除非该值是一个承诺,在这种情况下,它使用 promise chaining.

在该承诺中解析的值来解析

有关详细信息,请参阅