1

Hi i'm trying to get the status of an http request and conditionally set a variable. I'm making a subcall to check if user1 is following user2. My code looks like this. (for brevity i cut the foreach function that needs to loop through a list of users that i have previously get requested reason why i have to push)

$scope.users = [];
  var getUser = function(id) {
        UserService.GetUserById(id, $localStorage.CurrentUser.auth_token)
        .success(function (data) {

         data = angular.fromJson(data);

//data model ==> {id: 0, username: "foo"}

//check if 404 or 200 ==> UserService.GetUserFollowers($stateParams.id, data.id)
//if 200 data.is_following = 1; if 404 data.is_following = 0

         $scope.users.push(data);  

//data model after pushed ==> {id: 0, username: "foo", is_following: 1}

         console.log(angular.toJson($scope.users));

        }).error(function(error, status) {
            alert(status);
            console.log(error);         
        });

    };

Tried this didn't work

$scope.users = [];
  var getUser = function(id) {
        UserService.GetUserById(id, $localStorage.CurrentUser.auth_token)
        .success(function (data) {

         $scope.data = angular.fromJson(data);

         UserService.GetUserFollowers($stateParams.id, $scope.data.id, -1, -1)
            .success(function(data, status) {                      
              $scope.status = status;
            }).error(function(data, status) {               
              $scope.status = status;
            });

       if ($scope.status === 200) {
          $scope.data.is_following = true;
       }else{
          $scope.data.is_following = false;
       }

        $scope.users.push($scope.data);

         console.log(angular.toJson($scope.users));

        }).error(function(error, status) {
            alert(status);
            console.log(error);         
        });

    };

this is my service:

  this.GetUserFollowers = function (my_id, to_id, begin, end) {
            return $http.get($rootScope.endPoint + '/user/' + my_id + '/followers/' + to_id + '/' + begin + '/' + end + '/');    
        };

!NOTE - begin and End are ignored if to_id is not -1

3
  • are you asking how to write the GetUserFollowers function so that it returns these status codes? If that is the function that you are trying to work out the logic for, it would be helpful to see the code you have for it now.... Commented Jul 13, 2015 at 0:53
  • @Claies check edit.. the edited code for some reason sets it true or false wrongly Commented Jul 13, 2015 at 2:57
  • what, like the status from your $http call comes back something other than HTTP 200 and $scope.data.is_following still ends up true somehow? Commented Jul 13, 2015 at 6:01

1 Answer 1

3

$http returns status code as the second argument.

$http.get(url)
  .success(function(data, status) {
      alert(status); // HTTP status code of the response
  })
  .error(function(data, status) {
      alert('Error with status code: ' + status); 
  });

However, if the status is an error status, such as 404, then the error block will be called.

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

1 Comment

.success and .error are deprecated and not available in Angular 1.6 anymore.

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.