Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading response headers when using $http of Angularjs

Tags:

angularjs

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 ?

like image 770
Joy Dutta Avatar asked Jun 11 '13 07:06

Joy Dutta


People also ask

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.

Can Javascript read response headers?

While you can't ready any headers of HTML response in JS, you can read Server-Timing header, and you can pass arbitrary key-value data through it.

Is HTTP request is synchronous or asynchronous in AngularJS?

The problem is as follows, $http. get is asynchronous, before the response is fetched, the function returns. Therefore the calling function gets the data as empty string.


1 Answers

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.

like image 123
Pablo Avatar answered Sep 21 '22 15:09

Pablo