5

Note: Turns out this is a duplicate: Accessing the web page's HTTP Headers in JavaScript

Is there a way to get the request headers for the index.html request? For example, this is a screenshot of me hitting google.com

enter image description here

I would like to be able to get access to the Request Headers. Is this possible?

What about for other javascript/css assets?

Community
  • 1
  • 1
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
  • You probably can't do this without violating the same-origin policy, but there could be workarounds as you want to get the request data, but wouldn't you already have this when you're sending the request ? – adeneo Mar 31 '15 at 13:12
  • possible duplicate of [Accessing the web page's HTTP Headers in JavaScript](http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript) – albanx Mar 31 '15 at 13:13
  • The initial load is done by the browser itself, I don't see a way for you to access those headers other than re-requesting them via javascript... – gtramontina Mar 31 '15 at 13:13
  • You can ping some ajax query for example. There is `jsonp` service for this: http://ajaxhttpheaders1.appspot.com/?callback=function(){} – vp_arth Mar 31 '15 at 13:13
  • you cannot access the `request header` in a normal http request. probably some of them are picked up by the
    tag of your html page
    – albanx Mar 31 '15 at 13:16

2 Answers2

-1

It depends on how are you requesting that page, if you are loading it over ajax with javascript/jquery it is possible:

$.ajax({
    type: 'GET',
    url:'google.com',
    success: function(resp){
        console.log(resp.getAllResponseHeaders());
    }
});
vikram raj
  • 37
  • 8
taxicala
  • 21,408
  • 7
  • 37
  • 66
  • Nope, just used that as an example. I want to be able to get the accept-language header without making an additional http request. – kentcdodds Mar 31 '15 at 13:13
  • 1
    Can't be done with pure html, you won't be able to read the headers. If you serve your page over PHP for example you can have your headers printed, but when loading an external page if you dont use javascript you won't be able to get the headers. – taxicala Mar 31 '15 at 13:15
-1

You can try: window.navigator.userLanguage || window.navigator.language
Or also you can make ajax query.

There are services for this.
Ex: http://ajaxhttpheaders1.appspot.com/?callback=function(){} (JSONP)

vp_arth
  • 14,461
  • 4
  • 37
  • 66