如何使用Angularjs调用promise

时间:2015-08-31 12:30:06

标签: angularjs http post

我使用angularjs执行HTTP POST请求,我只是希望它在页面加载期间调用它。但POST的响应会返回一个承诺。

我打电话时如何解决承诺?如何在页面加载时获得已解决的响应?

1 个答案:

答案 0 :(得分:1)

据我所知,您在以角度发出POST请求时询问是否获得结果。请参阅此示例,取自angularjs docs https://docs.angularjs.org/api/ng/service/ $ http:

$http.post('/someUrl', {msg:'hello word!'}).
  then(function(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

拨打电话后,imadietelly无法使用结果。相反,方法post()会返回一个承诺。 Promise是js中异步编程的基本概念。在请求完成后,承诺会尽快得到解决。要使用结果,您需要通过适当的回调调用then()方法。将使用检索到的数据调用回调。