Question

How can I access the HTTP response status code after a successful request using Restangular?

CoffeeScript Example

Restangular.all('orders').getList().then (result) ->
    console.log result  # no status code
    console.log 'Preferably, status code should be here: ', result.status
, (error) ->
    console.log 'Error status code: ', error.status  # this works

I tried to implement a response extractor to tack it on as metadata, but the status code is already stripped by the time it flows into the extractor.

Était-ce utile?

La solution

You would use setFullResponse in your modules config to grab the status code on a successful request.

https://github.com/mgonto/restangular#setfullresponse

var app = angular.module('app', ['restangular']);

// Change setFullResponse to be true in the modules config.
app.config(['RestangularProvider', function (RestangularProvider) {
    RestangularProvider.setFullResponse(true);
}]);

// Using the new response format.
app.controller('someController', ['Restangular', function (Restangular) {
    Restangular.all('orders').getList().then(function (result) {
        // Response from the server.
        console.log(result.data);

        // Response status code.
        console.log(result.status);
    });
}]);

Coffeescriptified:

app = angular.module('app', ['restangular'])

// Change setFullResponse to be true in the modules config.
app.config ['RestangularProvider', (RestangularProvider) ->
    RestangularProvider.setFullResponse true
]

// Using the new response format.
app.controller 'someController', ['Restangular', (Restangular) ->
    Restangular.all('orders').getList().then (result) ->
        // Response from the server.
        console.log result.data

        // Response status code.
        console.log result.status
]

Hope this helps!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top