Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angularjs中,如何将第一个api的结果应用到第二个api调用中?_Javascript_Angularjs_Api - Fatal编程技术网

Javascript 在angularjs中,如何将第一个api的结果应用到第二个api调用中?

Javascript 在angularjs中,如何将第一个api的结果应用到第二个api调用中?,javascript,angularjs,api,Javascript,Angularjs,Api,我想使用第一个api的结果,进入第二个api调用。场景是这样的,我想使用第一个api的结果,进入第二个api调用。如果我是正确的,那么我希望同步api调用不确定。我试图编写以下函数,但不起作用。函数2是在函数1之前调用的。在function2中,我们使用result1,它只有在function2之前调用function1时才会出现,我是这样做的 $scope.function1 = function(){ var deferred= $q.defer();

我想使用第一个api的结果,进入第二个api调用。场景是这样的,我想使用第一个api的结果,进入第二个api调用。如果我是正确的,那么我希望同步api调用不确定。我试图编写以下函数,但不起作用。函数2是在函数1之前调用的。在function2中,我们使用result1,它只有在function2之前调用function1时才会出现,我是这样做的

$scope.function1 = function(){
        var deferred= $q.defer();    
        $http.post(api1, data1)
        .success(function(response, status) {
          if (response.error == 0) {
            console.log(response.result);          
               $scope.result1=response.result;      
          }           
        }) ;    
        deferred.resolve('Success') ; 
        return deferred.promise;       
    };
    var promise =  $scope.addDefaultValue();
    promise.then(function(){
      $scope.function2();
    });

     $scope.function2=function(){
        var deferred = $q.defer();

        $http.post(api2,result1)
        .success(function(response, status){
          if(response.error == 0){

          }
        });
      deferred.resolve('Success') ;
      return deferred.promise;
    }
无法将$http请求转换为同步请求。那不是迪雷尔做的。延迟是一种将不支持承诺的函数转换为支持承诺的函数的方法$http函数返回promise对象,因此不需要使用延迟对象

$http.post(api, data1).then(function (response) {
  $scope.result1 = response.data.result;
  // return is important here
  // so that you can keep chaining with .then
  return $http.post(api2, response.data.result);
}).then(function (response) {
  // here you have response from api2
  $scope.result2 = response.data.result;
  console.log(response.data);
}).catch(function (error) {
  // here you can handle errors
  // from either api calls
  // second api call won't be made if the first one fails
  console.log(error);
});

您可以在这里遵循承诺链模式,使用。然后在承诺对象上遵循链接

无需使用$q创建额外的开销承诺,因为$http方法在启动ajax时返回承诺对象

代码


响应是一个承诺对象。应该是访问响应。data@mostruash朱利安提出的方法有什么问题。http内部的http调用我猜朱利安删除了他的答案,所以我不知道。@geeks朱利安的答案并不是真的错,尽管成功被弃用,赞成使用朱利安建议的“什么是问题”方法。http内部http调用…@Julian为什么要删除您的答案,我的代码正在使用您的方法…可能是因为您的方法不好?就像有人问她如何关闭电脑,她试着打电话给电力公司,但没有结果。然后其中一个答案建议她取消与电力公司的合同,只是关闭她的电脑。答案是正确的,因为它是有效的。
$scope.function1 = function() {
  return $http.post(api1, data1)
    .then(function(d) {
    var response = d.data;
    if (response.error == 0) {
      console.log(response.result);
      $scope.result1 = response.result;
    }
    return response;
  });
};

$scope.function1().then(function(data) {
  $scope.function2();
}, function(error) {

});