如何在 AngularJS 中使用重试逻辑重用 HTTP 请求 [英] How to reuse HTTP request with retry logic in AngularJS

查看:22
本文介绍了如何在 AngularJS 中使用重试逻辑重用 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 AngularJS 中多次执行相同的 HTTP 请求?即没有重新定义相同的请求两次?

Is it possible to execute the same HTTP request more than once in AngularJS? i.e. without re-defining the same request twice?

var retry = false;
var req = $http.get( 'ajax.php?a=StartSession&ref=' + code );
req.success(function(res) {
    alert(res);
});
req.error( function(res) {
    if(retry == false)
       //run request again req.get(); ?
    retry = true;
});

推荐答案

服务和工厂的用途:

app.factory("dataFactory", ["$http", function($http) {
    return {
        call: function(code) {
            return $http.get( 'ajax.php?a=StartSession&ref=' + code )
        }
    }
}]);

注入使用

app.controller("myCtrl", ["dataFactory", function(dataFactory) {
    var code = "myCode";
    dataFactory.call(code).success(function(res) {
        //gotcha
    });
}]);

这篇关于如何在 AngularJS 中使用重试逻辑重用 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆