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

时间:2014-01-27 10:29:58

标签: coffeescript http-status-codes 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

我尝试将响应提取器实现为元数据,但状态代码在流入提取器时已被剥离。

1 个答案:

答案 0 :(得分: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);
    });
}]);

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
]

希望这有帮助!