How to read response headers in angularjs?

前端 未结 7 1329
终归单人心
终归单人心 2020-11-27 17:17

My server returns this kind of header: Content-Range:0-10/0:

\"enter

相关标签:
7条回答
  • 2020-11-27 17:27

    Use the headers variable in success and error callbacks

    From documentation.

    $http.get('/someUrl').
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
      })
      .error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    

    If you are on the same domain, you should be able to retrieve the response headers back. If cross-domain, you will need to add Access-Control-Expose-Headers header on the server.

    Access-Control-Expose-Headers: content-type, cache, ...
    
    0 讨论(0)
  • 2020-11-27 17:30

    Why not simply try this:

    var promise = $http.get(url, {
        params: query
    }).then(function(response) {
      console.log('Content-Range: ' + response.headers('Content-Range'));
      return response.data;
    });
    

    Especially if you want to return the promise so it could be a part of a promises chain.

    0 讨论(0)
  • 2020-11-27 17:36

    According the MDN custom headers are not exposed by default. The server admin need to expose them using "Access-Control-Expose-Headers" in the same fashion they deal with "access-control-allow-origin"

    See this MDN link for confirmation [https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers]

    0 讨论(0)
  • 2020-11-27 17:40

    Additionally to Eugene Retunsky's answer, quoting from $http documentation regarding the response:

    The response object has these properties:

    • data{string|Object} – The response body transformed with the transform functions.

    • status{number} – HTTP status code of the response.

    • headers{function([headerName])} – Header getter function.

    • config{Object} – The configuration object that was used to generate the request.

    • statusText{string} – HTTP status text of the response.

    Please note that the argument callback order for $resource (v1.6) is not the same as above:

    Success callback is called with (value (Object|Array), responseHeaders (Function), status (number), statusText (string)) arguments, where the value is the populated resource instance or collection object. The error callback is called with (httpResponse) argument.

    0 讨论(0)
  • 2020-11-27 17:43

    The response headers in case of cors remain hidden. You need to add in response headers to direct the Angular to expose headers to javascript.

    // From server response headers :
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, 
    Content-Type, Accept, Authorization, X-Custom-header");
    header("Access-Control-Expose-Headers: X-Custom-header");
    header("X-Custom-header: $some data");
    
    var data = res.headers.get('X-Custom-header');
    

    Source : https://github.com/angular/angular/issues/5237

    0 讨论(0)
  • 2020-11-27 17:45

    Updated based on Muhammad's answer...

    $http.get('/someUrl').
      success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        console.log(headers()['Content-Range']);
      })
      .error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    
    0 讨论(0)
提交回复
热议问题