连接 $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');
});
}
您正在覆盖第一个回调中的 $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');
});
});
}
或者您也可以在调用中显式使用 $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 (.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');
});
}
你差一点就错过了
$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');
});
})