0

I can't access the output variable from my 1st http get request, i need this data for another http Post request.

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) {});

}

I need the $scope.osChild to be present in my http post request.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
aintno12u
  • 341
  • 4
  • 23

2 Answers2

3

Simply chain the two XHRs:

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)
        });
    });
};

The .then method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining.

For more information, see

georgeawg
  • 48,608
  • 13
  • 72
  • 95
-1

first GET call is asynchronous so $scope.osChild setting null initially. so suggestion is to use 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) {});

  });

};
Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34
  • Avoid the [deferred anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). The `.success` method has [been removed from the AngularJS framework](https://stackoverflow.com/a/35331339/5535245). – georgeawg Jun 20 '19 at 11:09