Get response header in then() function of a ngResource object's $promise property after resource resolved?

后端 未结 3 1461
醉梦人生
醉梦人生 2021-02-07 23:37

I\'m willing to retrieve the response header of a resource request, cause I\'ve put pagination information and something else in it rather than the response body, to make the RE

3条回答
  •  终归单人心
    2021-02-08 00:21

    I had the exact same problem. I used an interceptor in the resource definition to inject the http headers in the resource.

    $resource('/api/resource/:id', {
        id: '@id'
      }, {
        index: {
          method: 'GET',
          isArray: true,
          interceptor: {
            response: function(response) {
              response.resource.$httpHeaders = response.headers;
              return response.resource;
            }
          }
        }});
    

    Then, in the then callback, the http headers are accesible through $httpHeaders:

    promise.then(function(resource) {
        resource.$httpHeaders('header-name');
    });
    

提交回复
热议问题