0

I need the status code of my factory with promises, How can I do get it?

myApp.factory('cartService', ['$resource', function($resource){
  return $resource('/api/v1/carts ', {}, {
    show: { method: 'GET', isArray:false, headers:{'Authorization':'Token token=' + localStorage.Token} },
    create: { method: 'POST', headers:{'Authorization':'Token token=' + localStorage.Token} }
  })
}]);

This is the call

cartService.show().$promise.then(function(data){
        $rootScope.cart = data;
        // GET STATUS ????
      }).catch(function(response){
        growl.warning("Problems!!", {title: 'Error'});
      })

The Response has status 204

enter image description here

Israel Barba
  • 1,434
  • 20
  • 28
  • 1
    Possible duplicate of [How do I get the HTTP response status code in AngularJS 1.2](http://stackoverflow.com/questions/18729556/how-do-i-get-the-http-response-status-code-in-angularjs-1-2) – Matt C Apr 19 '16 at 19:52

1 Answers1

0

As said in this answer,

You have to add an interceptor to your $ressource call. In interceptor, you can add $http response to the promise response.

Code is like this:

var resource = $resource(url, {}, {
    get: {
        method: 'GET'
        interceptor: {
            response: function(response) {      
                var result = response.resource;        
                result.$status = response.status;
                return result;
            }
        }
    }                            
});

I suggest you to check original answer.

Community
  • 1
  • 1
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • If you find another question that has the answer the poster is looking for, you should consider flagging this question as a duplicate of that question instead of posting the original answer here. – Matt C Apr 19 '16 at 19:53