使用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

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

最佳答案

您可以在模块配置中使用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);
    });
}]);


咖啡:

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
]


希望这可以帮助!

08-08 02:25