0

I have changed the env property of my url to test my code locally.. It seems the error call back is not even getting called. My code snippet-

        $scope.getfiles = function() {
        Api.file.query().$promise.then(
        function(result){ $scope.getfiles = result; },  //on success
        $scope.commonAjaxErrorHandling("Failed to get  File data.",true)  //on failure--> Always this line of  code is executing..
           );
         };

If I try writing other error function. It is not even getting called.

        $scope.getfiles = function(){
        Api.flatFile.query().$promise.then(
         function(result){ $scope.File = result; 
  },
      function (result) { // this block is not getting called
         if(result.status== 404){
         $scope.addErrorAlert("Service is down.Please try again later",true);
         return;
     }
 });
};

Can someone help me out this situation?

        FileServices.factory('Api', ['$res', '$loc',
        function($res, $loc){
        var contextPath = $loc.absUrl().split('/app/')[0];
        return {
        flatFile: $res(contextPath+'/app/config/flatFile/data', {}, {
        query: {method:'GET', isArray:true},
        save: {method:'POST'},
        })
        };
       }]);
5
  • could you verify that function getting called or not debugging it through developer console.? is there any error in console> Commented Jan 10, 2016 at 13:38
  • The angular promise library is $q Commented Jan 10, 2016 at 14:02
  • What is API.file.query(), and what are you wanting to achieve with it, please provide the code portion. Commented Jan 10, 2016 at 14:26
  • Please see my edited post.. Commented Jan 10, 2016 at 14:35
  • API is a a service. and am trying to add error alert message when service is down. So locally i tried to change env property of url. Commented Jan 10, 2016 at 14:37

2 Answers 2

1

The function probably doesn't get called in either way. The problem here is that in the first case you do not define a function as a callback but immediately call the function itself

$scope.commonAjaxErrorHandling("Failed to get  File data.",true)

this is not a callback-definition but the immediate call of $scope.commonAjaxErrorHandling

So my idea is that no error occurs and thats why in the second case the error-function isn't called either

Sign up to request clarification or add additional context in comments.

Comments

0

It seems like you are checking for the status incorrectly. You should be accessing the second parameter of the fail callback:

  // ...
  ,
  function (jqXHR, textStatus, error) { 
     if(textStatus == 404){
        $scope.addErrorAlert("Service is down.Please try again later",true);
        return;
   }
 }

Note: There is no file key in the object that you're returning from Api service, and therefore it should be flatFile instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.