javascript - AngularJS How to prevent duplicated http requests? - Stack Overflow

I'm struggling for the past day with some weird situation.What's happening is that for an ht

I'm struggling for the past day with some weird situation. What's happening is that for an http request to an API on a remote server occasionally duplicate requests are being sent. Can anyone please provide help on how to avoid these duplicated requests?

Here is an example of a function that I use on a factory:

factory.getAllConsultedClientsLogs = function(oParams) {

    var deferred = $q.defer();

    $http.post('url/to/api', oParams)
        .success(function(response) {
            deferred.resolve(response);
        })
        .error(function() {
            deferred.reject("Error! @factory.getAllConsultedClientsLogs");
        });

return deferred.promise;

};

...and an example of a function using the above indicated on a controller:

$scope.openConsultedClientsLogsModal = function(operator, date) {

    if (angular.isDefined(operator) && angular.isDefined(date)) {

        RrAuditingFactory.getAllConsultedClientsLogs({'operator':operator,'date':date}).then(function(promise) {

            if (angular.isObject(promise) && angular.isDefined(promise.error) && promise.error == 0) {

                var modalInstance = $modal.open({
                    templateUrl: 'path/partial',
                    controller: function($scope, $modalInstance, logsResult) {
                        $scope.logsResult = logsResult;
                    },
                    resolve: {
                        logsResult: function() {
                            return promise.result;
                        }
                    }
                });

                modalInstance.result.then(function() {
                }, function () {
                });

            } else {
                ErrorContext.setError(promise.errorMsg);
            }

        }, function(promise) {
            ErrorContext.setError(promise);
        });

    } else {

        ErrorContext.setError();

    }

};

Thank you in advance.. hope that anyone could help me out...

I'm struggling for the past day with some weird situation. What's happening is that for an http request to an API on a remote server occasionally duplicate requests are being sent. Can anyone please provide help on how to avoid these duplicated requests?

Here is an example of a function that I use on a factory:

factory.getAllConsultedClientsLogs = function(oParams) {

    var deferred = $q.defer();

    $http.post('url/to/api', oParams)
        .success(function(response) {
            deferred.resolve(response);
        })
        .error(function() {
            deferred.reject("Error! @factory.getAllConsultedClientsLogs");
        });

return deferred.promise;

};

...and an example of a function using the above indicated on a controller:

$scope.openConsultedClientsLogsModal = function(operator, date) {

    if (angular.isDefined(operator) && angular.isDefined(date)) {

        RrAuditingFactory.getAllConsultedClientsLogs({'operator':operator,'date':date}).then(function(promise) {

            if (angular.isObject(promise) && angular.isDefined(promise.error) && promise.error == 0) {

                var modalInstance = $modal.open({
                    templateUrl: 'path/partial',
                    controller: function($scope, $modalInstance, logsResult) {
                        $scope.logsResult = logsResult;
                    },
                    resolve: {
                        logsResult: function() {
                            return promise.result;
                        }
                    }
                });

                modalInstance.result.then(function() {
                }, function () {
                });

            } else {
                ErrorContext.setError(promise.errorMsg);
            }

        }, function(promise) {
            ErrorContext.setError(promise);
        });

    } else {

        ErrorContext.setError();

    }

};

Thank you in advance.. hope that anyone could help me out...

Share Improve this question edited Jun 7, 2016 at 12:03 Abhishek 1435 bronze badges asked Feb 5, 2014 at 18:17 aquaaqua 3874 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

i have faced this problem, and you can resolve it like this :

check if you have declared ng-controller twice , you need to declare it just one time check if you have declared data-ng-click , if so , you need to replace it with ng-click that's it

I saw your link:

    $scope.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {

        $('td:eq(4)', nRow).bind('click', function() {
            $scope.$apply(function() {
                $scope.openConsultedClientsLogsModal(aData.Operator, aData.LogDate);
            });
        });

        return nRow;
    };

You can unbind before doing the bind, this way you will prevent duplicates. Try like this:

    $scope.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {



        //add this unbind to your code
        $('td:eq(4)', nRow).unbind("click");


        $('td:eq(4)', nRow).bind('click', function() {
            $scope.$apply(function() {
                $scope.openConsultedClientsLogsModal(aData.Operator, aData.LogDate);
            });
        });

        return nRow;
    };

I hope this helps.

I tend to return an object from a factory, so in your case I'd do something like:

module.factory('clientsLogs', function($q, $http) {

  return {
    getAllConsulted: function(oParams) {
      var deferred = $q.defer();
      $http.post('url/to/api', oParams)
        .then(function(response) {
            deferred.resolve(response);
        }, function() {
            deferred.reject("Error! @factory.getAllConsultedClientsLogs");
        });

      return deferred.promise;
    }
  }

});

and then in your controller something like:

module.controller('MyCtrl', function($scope, clientsLogs) {
  clientLogs.getAllConsulted({}).then(function(){...})
}

Might not help you but I've never had any problems with duplicate calls doing it this way.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744363348a4570578.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信