【问题标题】:How can I get status code using $resource?如何使用 $resource 获取状态码?
【发布时间】:2017-11-13 22:14:54
【问题描述】:

我的请求工厂在这里:

angular.module('myapp').factory('testResponse',
        ['$http', '$resource', 'AppConfig', '$routeParams', '$rootScope',
            function($http, $resource, $routeParams, $rootScope) {
                $http.defaults.headers.common['Authorization'] = authorizationHeader;
                $http.defaults.headers.post['Content-Type'] = 'application/json';
                return $resource('test.json'), {}, {
                    query: {method: 'GET'}
                };
            }]);

控制器中的代码在这里:

angular.module('myapp').controller('TestCtrl',
        ['$http', '$scope', 'testResponse', 'AppConfig', function TestCtrl($http, $scope, testResponse) {
                testResponse.query(function(data) {
                    console.log(data.status);
                })
            }]);

理想情况下,它应该记录 $http 请求中的状态,但我无法为 $reource 获取它

【问题讨论】:

  • 重点是处理成功的资源请求时不需要状态。至于日志记录:拦截器似乎是更合适的工具。但你是对的:状态不可用。
  • 如何检查 404、500 或 403 等案例
  • 这些都是失败案例。您在故障处理程序中处理它们,并且您可以访问状态。您的示例仅使用成功处理程序。
  • 感谢@zeroflagL!我试过This snippet,但我仍然无法处理失败的情况。它导致了另一个错误“未定义”,即 $promise 不是一个函数,当我注入它时,我得到了 Unknown provider $promiseprovider
  • 另一个为什么不应该使用$resource的例子...访问成功时的状态码并不是一个不寻常的场景...这些数据应该默认可用,而不是对你隐藏

标签: angularjs angularjs-resource


【解决方案1】:

在服务testResponse 中,您可以将您的退货声明更改为此

return $resource('test.json'), {}, {
    query: {
        method: 'GET',
        transformResponse: function(data, headers,statusCode) {
            console.log(statusCode);//prints 200 if nothing went wrong
            var finalRsponse = {
                data: data,
                responseStatusCode: statusCode
            };
        return finalRsponse;
    }}
};

并且在您的控制器的 testResponse 服务 then(success,error) 的成功方法中,您可以使用 data.responseStatusCode 访问状态代码。

我已经在 angularjs-1.2.321.5.7 上测试过了。

【讨论】:

    【解决方案2】:
    ['$http', '$resource', 'AppConfig', '$routeParams', '$rootScope',
            function($http, $resource, $routeParams, $rootScope) {
    

    你错过了AppConfig参数。

    【讨论】:

      【解决方案3】:

      我尝试使用带有 $q 的 Promise 来处理这种我必须对失败或成功进行更多控制的情况。 我在这里重构了工厂:

      var defObj = $q.defer();
        var testResponse = $resource('https://jsonplaceholder.typicode.com/posts/1', {}, {
          query: {
            method: 'GET'
          }
        });
        testResponse.query().$promise.then(function(data) {
          //you can add anything else you want inside this function
          defObj.resolve(data);
          console.log(defObj, data);
        }, function(error) {
          //you can add anything else you want inside this function
          console.error("Service failure: " + error);
        });
        return defObj.promise;
      }
      

      这里是this pen 中的完整解决方案(使用mock json来模拟响应)

      【讨论】:

      • 我无法获得确切的服务器状态代码,但这种解决方法让我能够查找错误并获得更多控制
      • 请参阅下面我的回答以获取确切的服务器状态代码。
      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多