加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在AngularJs中链接Ajax调用

发布时间:2020-12-17 08:39:37 所属栏目:安全 来源:网络整理
导读:我想在一个链中进行多个Ajax调用。但是我也想在每次呼叫之后按下数据,然后进行下一个呼叫。最后,当所有调用成功时,我想运行一些其他代码。 我使用Angular $ http服务为我的Ajax调用,并希望坚持。 可能吗? 是的,这是由AngularJS处理非常优雅,因为它的$
我想在一个链中进行多个Ajax调用。但是我也想在每次呼叫之后按下数据,然后进行下一个呼叫。最后,当所有调用成功时,我想运行一些其他代码。

我使用Angular $ http服务为我的Ajax调用,并希望坚持。

可能吗?

是的,这是由AngularJS处理非常优雅,因为它的$ http服务是围绕PromiseAPI构建的。基本上,对$ http方法的调用返回一个promise,你可以通过使用then方法很容易地链接promises。这里是一个例子:
$http.get('http://host.com/first')
   .then(function(result){
    //post-process results and return
    return myPostProcess1(result.data); 
   })
   .then(function(resultOfPostProcessing){
    return $http.get('http://host.com/second'); 
   })
   .then(function(result){
    //post-process results of the second call and return
    return myPostProcess2(result.data); 
   })
   .then(function(result){
      //do something where the last call finished
   });

你也可以结合后处理和下$ $函数,这一切都取决于谁是对结果感兴趣。

$http.get('http://host.com/first')
   .then(function(result){
    //post-process results and return promise from the next call
    myPostProcess1(result.data); 
    return $http.get('http://host.com/second'); 
   })
   .then(function(secondCallResult){
     //do something where the second (and the last) call finished
   });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读