Javascript 连接$http.post请求
如何连接两个$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
$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){…