簡體   English   中英

連接$ http.post請求

[英]concatenate $http.post requests

如何連接兩個$ http(.post)請求,以便一個在另一個之后被調用? 這是我的功能,似乎無法正常工作。

$scope.signup = function() {
    console.log("New user");   
    $http.post('/signup',{
        email: this.email,
        password: this.password
    }).then(function success($http){
         return $http.post('api/userData/init'); //error
    }).then(function redirect($state){
        return $state.go('profile');
    });            

}

你只差一點就錯過了

$scope.signup = function() {
    console.log("New user");   
    $http.post('/signup',{
        email: this.email,
        password: this.password
    }).then(function(data){
        $http.post('api/userData/init').then(function(newData){
            $state.go('profile');
        });
    });

}

或者,您也可以在調用中顯式使用$ q Promises:

//$q needs to be injected in the controller or service
   var q = $q.deffer();

   $http.post('/signup',{
        email: this.email,
        password: this.password
    }).then(function(data){
        //on success
       q.resolve(data);
    }, function(){
        //on fail
        q.reject('Failed');
    });

    //second call after the first is done
    q.promise.then(function(firstCalldata){
        //if you need you can use firstCalldata here
        $http.post('api/userData/init').then(function(newData){
            $state.go('profile');
        });
    })

您在第一個回調中覆蓋$http then函數需要一個以http調用的結果作為參數的函數。 嘗試:

$scope.signup = function() {
    console.log("New user");   
    $http.post('/signup',{
        email: this.email,
        password: this.password
    }).then(function(){
         return $http.post('api/userData/init'); 
    }).then(function (){
        return $state.go('profile');
    });            

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM