$ http.post调用另一个$ http.post

时间:2017-04-12 18:39:17

标签: javascript angularjs angular-promise

我想在另一个$http.post内建一个$http.post,因为第二个依赖于第一个$http.post("/my/server/location").then(function (response) { $http.post("/my/second/api/call/"+response.data.number).then(function (response) { $scope.message = "Created successfully"; }, function (response){ $scope.message = "Could not create"; }); //Create modal from response.data received from first API Call //Add $scope.message to this modal. }); 。基本上我正在做的是:

$scope.message

正如您所猜测的那样,第二个问题仍然存在,直到返回初始承诺为止,我想在模态中显示$q,因此是不可能的。虽然我理解为什么会这样,但我似乎无法想象如何解决这个问题。我尝试与{{1}}打交道,但弄得一团糟。任何帮助都将很高兴。

4 个答案:

答案 0 :(得分:2)

,消息在成功处理程序和第二个XHR的拒绝处理程序中都使用return statement

$http.post("/my/server/location").then(function (response1) {
    return $http.post("/my/second/api/call/"+response1.data.number)
      .then(function (response2) {
        $scope.message = "Created successfully";
        return $scope.message;
    }).catch(function (errorResponse2) {
        $scope.message = "Could not create";
        return $scope.message;
    }).then(function(message) {

        //Create modal from response1.data received from first API Call
        //Add `message` from the second XHR to this modal.

        return [response1.data, message];
    });
});

.catch拒绝处理程序中的return statement会将被拒绝的承诺转换为成功,该成功由链中的下一个.then方法处理。

最终的promise会返回一个数组,其中包含第一个XHR的数据和第二个XHR的消息。

  

Chaining Promises

     

因为调用promise的.then方法会返回一个新的派生promise,所以很容易创建一个promise链。

     

可以创建任意长度的链,因为可以使用另一个承诺来解决承诺(这将进一步推迟其解决方案),可以暂停/推迟对承诺的解决链中的任何一点。这使得实现强大的API成为可能。

     

— AngularJS $q Service API Reference - Chaining Promises

答案 1 :(得分:0)

你是对的,$q在这里不会有所帮助,除非你想要同时执行两个承诺,然后等待他们的两个回复。

这里你要执行一个承诺然后另一个承诺,根据你的代码看起来很好。但是,在第二个承诺执行后您想要执行的任何操作都需要在.then()块中完成。

示例:

$http.post("/my/server/location").then(function (response) {
    $http.post("/my/second/api/call/"+response.data.number).then(function (response) {
       $scope.message = "Created successfully";
       // Create modal from response.data received from first API Call
    }, function (error){
        $scope.message = "Could not create";
    });
});

答案 2 :(得分:0)

$http.post("/my/server/location")
 .then(function (response) {

    $http.post("/my/second/api/call/"+response.data.number).then(function 
      (response) {

     }, function (response){

     });

    $scope.message = "Created successfully";

     //Create modal from response.data received from first API Call
    //Add $scope.message to this modal.
    $scope.buildModal(response.data);

})
.catch(function(error){
    $scope.message = "Could not create";
    $scope.buildModal();
})

所以你所拥有的问题是$scope.message = "Created successfully";被锁定在嵌套的api调用中。通过取消它,你可以在第一次api调用后立即建立你的模态。在上面的代码中,没有任何东西在等待第二个api调用完成,因为.then块中没有任何内容

我将模态构建移动到单独的函数buildModal,因为它必须从.then.catch块调用

答案 3 :(得分:-1)

试试这个

var fn = {
    test: function() {
        return $http.post("/my/server/location");
    }
};

fn.test().success(function(response) {
  $http.post("/my/second/api/call/"+response.data.number).then(function (response) {
      $scope.message = "Created successfully";
    }, function (response){
      $scope.message = "Could not create";
      }
});