artworkad シ artworkad シ - 1 month ago 7x
AngularJS Question

How to read response headers in angularjs?

My server returns this kind of header:


enter image description here

I tried to read this header in angular with no luck:

var promise = $http.get(url, {
params: query
}).then(function(response) {

which just prints

Object {content-type: "application/json; charset=utf-8"}

Any ideas how to access the content range header?


Use the headers variable in success and error callbacks

From documentation.

  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, ...