AngularJS 使 http post 与 then 同步
AngularJS make http post synchronous order with then
我有一个 http post 到一个 returns 一个 json 的文件然后我使用 .then(function(response){}.... 但我怎么不能得到另一个 http post 连续发生,然后继续第二个 .then?
我想要像
这样的东西
$http({
method:"POST",
url:"....",
data:{'....}).then(function(response){...})
$http({
method:"POST",
url:"....",
data:{'....}).then(function(response){...})
但是取决于它的另一个而不是像下面那样异步发生我想要一些东西
$http({
method:"POST",
url:"....",
data:{'....})First_then().then(function(response){...})
更新
我想出明天我会 post 为将来遇到同样问题的每个人解决它实际上很简单!
您想从第一个
调用第二个 $http
类似于:
function secondRequest(dataFromFirst) {
const data = {
someProp: dataFromFirst.someprop
}
return $http({
method: "POST",
url: "....",
data: data
}).then(function(response) {
// return some combination of new response and dataFromFirst
})
}
$http({
method: "POST",
url: "....",
data: {}
})
.then(secondRequest)
.then(function(combinedData) {
// combined data from secondRequest then
})
问题
需要两个相互依赖的请求作为第二个输入。
必须按顺序完成,以便主体在返回正确的 json 文件后可以正确执行
解决方案
我不得不使用承诺 q.all
首先作为控制器注入区域的参数,如
bookingUpdateView.controller('liveBookingController',['$scope',
'$http', '$q', function($scope, $http, $q){
.... code here of entire controller....
}]);
然后在控制器内部我做了 2 个承诺作为 $scope 函数和 RETURN 或者我可以简单地把它们放在 $q.all([]) 中。然后但这非常混乱所以
$scope.promise = function(i){
return $http({
method:"POST",
url:"model/fetch_data_booking_view_seats_available.php",
data:{'movie_name':$scope.movieTitle,
'theatre_n':$scope.list_TP_5[i].theatre_name_fk,
..... etc.....}
})
}
接下来是队列本身,我有一个主体可以在另一个 $scope 函数中执行,例如
$scope.updateTimePlay = function (id){
//..... code here .......
$q.all([$scope.promise(i),
$scope.promise1(i)])
.then(function(responses){
//....body here......
//in order to get JSON properly had to do
$scope.SeatsSub = responses.map((resp) => resp.data);
//so i could call it like this
$scope.SeatsSub[1][0].seatsAvailable;
//or use output to see what going on
console.log($scope.SeatsSub)
})
//....code here .....
}
我有一个 http post 到一个 returns 一个 json 的文件然后我使用 .then(function(response){}.... 但我怎么不能得到另一个 http post 连续发生,然后继续第二个 .then? 我想要像
这样的东西 $http({
method:"POST",
url:"....",
data:{'....}).then(function(response){...})
$http({
method:"POST",
url:"....",
data:{'....}).then(function(response){...})
但是取决于它的另一个而不是像下面那样异步发生我想要一些东西
$http({
method:"POST",
url:"....",
data:{'....})First_then().then(function(response){...})
更新
我想出明天我会 post 为将来遇到同样问题的每个人解决它实际上很简单!
您想从第一个
调用第二个$http
类似于:
function secondRequest(dataFromFirst) {
const data = {
someProp: dataFromFirst.someprop
}
return $http({
method: "POST",
url: "....",
data: data
}).then(function(response) {
// return some combination of new response and dataFromFirst
})
}
$http({
method: "POST",
url: "....",
data: {}
})
.then(secondRequest)
.then(function(combinedData) {
// combined data from secondRequest then
})
问题 需要两个相互依赖的请求作为第二个输入。 必须按顺序完成,以便主体在返回正确的 json 文件后可以正确执行
解决方案 我不得不使用承诺 q.all 首先作为控制器注入区域的参数,如
bookingUpdateView.controller('liveBookingController',['$scope',
'$http', '$q', function($scope, $http, $q){
.... code here of entire controller....
}]);
然后在控制器内部我做了 2 个承诺作为 $scope 函数和 RETURN 或者我可以简单地把它们放在 $q.all([]) 中。然后但这非常混乱所以
$scope.promise = function(i){
return $http({
method:"POST",
url:"model/fetch_data_booking_view_seats_available.php",
data:{'movie_name':$scope.movieTitle,
'theatre_n':$scope.list_TP_5[i].theatre_name_fk,
..... etc.....}
})
}
接下来是队列本身,我有一个主体可以在另一个 $scope 函数中执行,例如
$scope.updateTimePlay = function (id){
//..... code here .......
$q.all([$scope.promise(i),
$scope.promise1(i)])
.then(function(responses){
//....body here......
//in order to get JSON properly had to do
$scope.SeatsSub = responses.map((resp) => resp.data);
//so i could call it like this
$scope.SeatsSub[1][0].seatsAvailable;
//or use output to see what going on
console.log($scope.SeatsSub)
})
//....code here .....
}