Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 连接$http.post请求_Javascript_Angularjs_Angular Promise - Fatal编程技术网

Javascript 连接$http.post请求

Javascript 连接$http.post请求,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,如何连接两个$http(.post)请求,以便一个接一个地调用? 这是我的功能,似乎不起作用 $scope.signup = function() { console.log("New user"); $http.post('/signup',{ email: this.email, password: this.password }).then(function success($http){ return $ht

如何连接两个$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');
    });            

}

您正在第一次回调中重写
$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');
    });            

}

您正在第一次回调中重写
$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');
    });            

}

你差一点就错过了

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

}

你差一点就错过了

$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承诺:

//$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');
        });
    })

或者,您也可以在通话中明确使用$q承诺:

//$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');
        });
    })

怎么了?您需要在第二个post上使用回调函数-
。然后(函数成功($http){…
发生了什么错误?您需要在第二个post上使用回调函数-
。然后(函数成功($http){…