Using ngResource
in AngularJS 1.2rc(x), how do I get the status code now?
RestAPI.save({resource}, {data}, function( response, responseHeaders ) {
});
where RestAPI
is my ngResource
.
The response has the $promise
object and the resource returned from the server but not a status anymore. The responseHeaders()
function only has a status if the server injects the status code into the header object, but not the true returned status code. So some servers may serve it and some might not.
For anyone using a newer version of Angular, looks like we've had access to the status code as a 3rd param to the
transformResponse
function since angular 1.3, but it was never documented properly in the $resource docs.I'm using AngularJS v1.5.6, and I do it like this (in my case I put the "getData" method inside a service):
then in the controller (for example), call that like this:
As documentation says, the response object has these properties:
See https://docs.angularjs.org/api/ng/service/$http
Hope it helps!