成功Restangular请求时访问HTTP状态代码

Pet*_*ron 5 http-status-codes coffeescript restangular

使用Restangular成功请求后,如何访问HTTP响应状态代码?

CoffeeScript示例

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
Run Code Online (Sandbox Code Playgroud)

我试图实现一个响应提取器来将其作为元数据添加,但状态代码在流入提取器时已被剥离.

JDW*_*dle 14

您可以在模块配置中使用setFullResponse来获取成功请求的状态代码.

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);
    });
}]);
Run Code Online (Sandbox Code Playgroud)

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
]
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!