0

假设我有一个具有此方法的角度服务:

this.query = function(params, successFn, errorFn) {
        return $resource(backendUrl + '/myresource.json')
        .query(params, successFn, errorFn);
};

有时其余 API 会失败,因此会调用 errorFn。但是,有时我可能会发送无效参数,而 Rest API 会发送一个HTTP 200with success : false。我知道这是一个不好的做法,但由于 API 不会被更改,我将在成功回调中处理错误。

你可能知道,成功回调总是给我一个数组,所以 JSON 响应:

{
    'message' : 'invalid parameter x',
    'success' : false
}

将作为 successFn 中的第一个参数公开为:

[ { 0 : 'i', '1' : 'n', '2' : 'v' .... }, {....} ]

有什么方法可以访问从 API 返回的原始内容?

4

1 回答 1

2

你可以使用$http代替资源

// $http returns a promise, which has a then function, which also returns a promise
var promise = $http({
    url: url,
    method: "GET",
    params: params
}).then(function (response) {
    //do what you want to do with the response here
    //to see what response contains print JSON.stringify(response)
});
于 2013-06-11T07:42:58.873 回答