Javascript 在另一个$http.post内调用$http.post
我想在另一个Javascript 在另一个$http.post内调用$http.post,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我想在另一个$http.post中创建一个$http.post,因为第二个依赖于第一个。基本上我正在做的是: $http.post("/my/server/location").then(function (response) { $http.post("/my/second/api/call/"+response.data.number).then(function (response) { $scope.message = "Created successfully";
$http.post
中创建一个$http.post
,因为第二个依赖于第一个。基本上我正在做的是:
$http.post("/my/server/location").then(function (response) {
$http.post("/my/second/api/call/"+response.data.number).then(function (response) {
$scope.message = "Created successfully";
}, function (response){
$scope.message = "Could not create";
});
//Create modal from response.data received from first API Call
//Add $scope.message to this modal.
});
正如您可能猜到的那样,在返回初始承诺之前,第二个承诺一直处于挂起状态,因此我想在模式中显示
$scope.message
,这是不可能的。虽然我理解为什么会发生这种情况,但我似乎不知道如何避免这种情况。我试着处理$q
,但结果弄得一团糟。任何帮助都将不胜感激。你说得对,$q
在这里不会有帮助,除非你希望两个承诺同时执行,然后等待他们的响应
在这里,您希望先执行一个承诺,然后执行另一个承诺,根据您的代码,这看起来很好。但是,在第二个承诺执行之后,您想要做的任何事情都需要在块中完成
例如:
$http.post("/my/server/location").then(function (response) {
$http.post("/my/second/api/call/"+response.data.number).then(function (response) {
$scope.message = "Created successfully";
// Create modal from response.data received from first API Call
}, function (error){
$scope.message = "Could not create";
});
});
你说得对,$q
在这里没有帮助,除非你想同时执行两个承诺,然后等待它们的响应
在这里,您希望先执行一个承诺,然后执行另一个承诺,根据您的代码,这看起来很好。但是,在第二个承诺执行之后,您想要做的任何事情都需要在块中完成
例如:
$http.post("/my/server/location").then(function (response) {
$http.post("/my/second/api/call/"+response.data.number).then(function (response) {
$scope.message = "Created successfully";
// Create modal from response.data received from first API Call
}, function (error){
$scope.message = "Could not create";
});
});
试试这个
var fn={
测试:函数(){
返回$http.post(“/my/server/location”);
}
};
fn.test().success(函数(响应){
$http.post(“/my/second/api/call/”+response.data.number)。然后(函数(response){
$scope.message=“创建成功”;
},功能(回应){
$scope.message=“无法创建”;
}
});
试试这个
var fn={
测试:函数(){
返回$http.post(“/my/server/location”);
}
};
fn.test().success(函数(响应){
$http.post(“/my/second/api/call/”+response.data.number)。然后(函数(response){
$scope.message=“创建成功”;
},功能(回应){
$scope.message=“无法创建”;
}
});
到链消息在第二个XHR的成功处理程序和拒绝处理程序中都使用a:
$http.post("/my/server/location").then(function (response1) {
return $http.post("/my/second/api/call/"+response1.data.number)
.then(function (response2) {
$scope.message = "Created successfully";
return $scope.message;
}).catch(function (errorResponse2) {
$scope.message = "Could not create";
return $scope.message;
}).then(function(message) {
//Create modal from response1.data received from first API Call
//Add `message` from the second XHR to this modal.
return [response1.data, message];
});
});
.catch
拒绝处理程序中的将被拒绝的承诺转换为成功,然后由链中的下一个方法处理
最后一个承诺返回一个数组,其中包含来自第一个XHR的数据和来自第二个XHR的消息
连锁承诺
因为调用承诺的.then
方法会返回一个新的派生承诺,所以很容易创建一个承诺链
可以创建任意长度的链,并且由于一个承诺可以用另一个承诺解决(这将进一步推迟其解决),因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能
要链接,请在第二个XHR的成功处理程序和拒绝处理程序中使用a:
$http.post("/my/server/location").then(function (response1) {
return $http.post("/my/second/api/call/"+response1.data.number)
.then(function (response2) {
$scope.message = "Created successfully";
return $scope.message;
}).catch(function (errorResponse2) {
$scope.message = "Could not create";
return $scope.message;
}).then(function(message) {
//Create modal from response1.data received from first API Call
//Add `message` from the second XHR to this modal.
return [response1.data, message];
});
});
.catch
拒绝处理程序中的将被拒绝的承诺转换为成功,然后由链中的下一个方法处理
最后一个承诺返回一个数组,其中包含来自第一个XHR的数据和来自第二个XHR的消息
连锁承诺
因为调用承诺的.then
方法会返回一个新的派生承诺,所以很容易创建一个承诺链
可以创建任意长度的链,并且由于一个承诺可以用另一个承诺解决(这将进一步推迟其解决),因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能
因此,您所遇到的问题是,$scope.message=“Created successfully”
被锁定在嵌套的api调用中。通过去掉它,您可以在第一个api调用后立即构建您的模式。在上面的代码中,没有任何东西等待第二个api调用完成,因为它的块中没有任何内容。然后
我将模式构建移动到一个单独的函数buildModal
,因为它必须从然后和catch
块调用
因此,您所遇到的问题是,$scope.message=“Created successfully”
被锁定在嵌套的api调用中。通过去掉它,您可以在第一个api调用后立即构建您的模式。在上面的代码中,没有任何东西等待第二个api调用完成,因为它的块中没有任何内容。然后
我将模式构建移到了一个单独的函数buildModal
,因为它必须从.then
和.catch
块中调用,所以问题是$scope.message仅在第二个api调用返回后定义?而您希望在第一个调用返回后定义吗?是的。@CameronRodriguezSo the问题是$scope.message仅在第二次api调用返回后定义?并且您希望在第一次调用返回后定义?是的,完全正确。@CameronRodriguezsuccess
已经被弃用了一段时间。success
已经被弃用了一段时间。谢谢Cameron,但这不会起作用。$scope.message
不是首选项第一个API调用的错误输出,但第二个API调用的错误输出。谢谢Cameron,但这不起作用。$scope.message
不是第一个API调用的错误输出,而是第二个API调用的错误输出。谢谢Ben,但这不起作用。第二个API不会总是返回200。这取决于用户输入。因此,如果返回200,然后存储成功消息,否则存储失败消息->使用d创建模式