Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Request header parsing


ghost's Avatar
0 0

Does anyone know a good way to get the request header details using javascript? I had a quick go with ajax and its xmlhttprequest class, but I couldnt get it to work for me. Its worth noting I want to read the request headers, not the response headers.

Id much prefer a simple javascript DOM affair, but other than document.cookie I cant seem to find one for the other request headers. The AJAX code im using now is:

<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("HEAD", "<url>",true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            alert(xmlhttp.getAllResponseHeaders());
            }
    }
xmlhttp.send(null)
</script>

Am I on the right track with AJAX, or am I gonna need something else?


ghost's Avatar
0 0

Ok, i found this on a site, not sure but worth a try

<script language="JavaScript"><!--
var xmlhttp				= new ActiveXObject("Microsoft.XMLHTTP");
var dateSent			= null; 
var dateComplete		= null;

function RetrieveHTTPHeaders(url)
{
	if (chkHEAD.checked)
		xmlhttp.open("HEAD", "http:\/\/" + url, true);         // Request just the header in an asynchronous call
	else
		xmlhttp.open("GET", "http:\/\/" + url, true);          // Request all in an asynchronous call
	
    xmlhttp.onreadystatechange = HandleState;
    xmlhttp.setRequestHeader("pragma", "nocache");
    dateSent = new Date();
    xmlhttp.send("");
}

function HandleState()
{

  var state = xmlhttp.readyState;
  
  if (state == 1)
  {
	btnSubmit.disabled = true;
	Status.innerText="";
	Result.innerText="";
	Body.innerText="";
	ResponseTime.innerText="";
  }
  else if (state == 4)
  {
	dateComplete = new Date();
    Status.innerText = xmlhttp.statusText + " (" + xmlhttp.status + ")";
    Result.innerText = xmlhttp.getAllResponseHeaders();
    Body.innerText = xmlhttp.responseText;
    ResponseTime.innerText = ((dateComplete.valueOf() - dateSent.valueOf())/1000) + " seconds";
	btnSubmit.disabled = false;    
  }
} 
--></script>

And this is the site http://www.whirlywiryweb.com/articles/getheaders.html


ghost's Avatar
0 0

Thanks for the reply, but im trying to actually understand this, not be a skid. Also,from my (very) limited knowledge of AJAX, that code doesnt do quite what I want, but it has some of the bits I was coding in it.


ghost's Avatar
0 0

Ok, does anyone know if it is at least possible to read request headers with JS?