Reading response headers when using $http of Angularjs

The question:

I am using $http to make an api call which is sending some custom header like X-Foo. However I can’t yet figure out how to read them. Inside the $http({...}).success(function (data, status, headers, config) {...}) function, headers is a function that should give me a hash of all headers but it only shows the header content-type. Is there a way to get the response headers ?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The custom headers will be visible in same domain. However, for the crossdomain situation, the server has to send Access-Control-Expose-Headers: X-Foo, ... header to make the custom headers visible.

Method 2

Spring 4.0+ provide @CrossOrigin annotation which has following parameters

  1. origins = list of Comma separated origin.
  2. exposedHeaders = list ofcomma separated count custom parameters.

example

@CrossOrigin(origins = "*", exposedHeaders ="X-Total-Count")


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment