My factory for making request is here:
angular.module('myapp').factory('testResponse',
['$http', '$resource', 'AppConfig', '$routeParams', '$rootScope',
function($http, $resource, $routeParams, $rootScope) {
$http.defaults.headers.common['Authorization'] = authorizationHeader;
$http.defaults.headers.post['Content-Type'] = 'application/json';
return $resource('test.json'), {}, {
query: {method: 'GET'}
};
}]);
The code in controller is here:
angular.module('myapp').controller('TestCtrl',
['$http', '$scope', 'testResponse', 'AppConfig', function TestCtrl($http, $scope, testResponse) {
testResponse.query(function(data) {
console.log(data.status);
})
}]);
Ideally it should log the status as in $http request but I am unable to get it for $reource
I tried use promises with $q to handle this kind of scenario where I had to have more control on failure or success. I refactored the factory like here:
Here is the complete solution in this pen (uses mock json to simulate the response)
You have missed
AppConfig
parameter.In service
testResponse
you can change your return statement to thisAnd in your controller's success method of then(success,error) of testResponse service you can access the status code using
data.responseStatusCode
.I have tested it on
angularjs-1.2.32
and1.5.7
.