9

Is there a way to get the header of the current page in angularjs, without having to do a separate call? I can't find this in the docs anywhere, but it seems like it should be possible.

E.g. if the file index.html contains the angular code, how do I read the header of the initial request to get index.html? I could use $http to make the call again, but that seems unnecessary.

Tyler
  • 11,272
  • 9
  • 65
  • 105
  • You can't access page headers via Javascript, without an ajax request, so you may be able to get the initial route, but not the index. – Dylan Oct 24 '14 at 22:44
  • Can't really understand your question. Can you elaborate? –  Oct 24 '14 at 22:44
  • 1
    @NoahMatisoff my angular code is referenced by an html file, which causes the script to be loaded. How do I get the header for that html file? For example in chrome debugger I can just click `Network` tab then select the HTML file and view the header. Is there a way to read this with angular? – Tyler Oct 24 '14 at 22:46
  • The question for index http headers duplicates - http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript. – Dylan Oct 24 '14 at 22:49
  • You are asking to have access to the response header of a http call that has been handled directly from the browser? How do you expect to have access to the request or response of an http call that your code isn't handling?? I mean, with plain JS you can't do that right? Then, what makes you think that you can do that with Angular? – Josep Oct 24 '14 at 22:50
  • @Dylan that involves another call to the server, which I would like to avoid if possible – Tyler Oct 24 '14 at 22:53
  • @Josep what about the header for the JS file itself? Is that available to the JS? – Tyler Oct 24 '14 at 22:53
  • 1
    @tyler same thing, if you are not handling the http request you won't have access to the request or the response, unless the code that it's making the request exposes that information somehow, which is not the case. So, I'm sorry but the answer is no. However, if I may: what is exactly what you need from the response header? Because there could be other ways to obtain the information of the headers (i.e. cookies) – Josep Oct 24 '14 at 22:59
  • @tyler I see, well then I'm sorry that the answer was no. – Josep Oct 24 '14 at 23:03

1 Answers1

4

If you are not handling the http request you won't have access to the request or the response, unless the code that it's making the request exposes that information somehow, which is not the case. So, I'm sorry but the answer is no.

However, if I may: what is exactly what you need from the response header? Because there could be other ways to obtain the information of the headers (i.e. cookies)

Josep
  • 12,926
  • 2
  • 42
  • 45
  • The use is for tracking environment variables. We can embed directly onto the page and then just read it, but was trying to go through headers if possible. Cookies are not an option. – Tyler Oct 24 '14 at 23:03