These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:12 pm Advanced search

Access the HTTP response headers via JavaScript

Here you can discuss things related to HTML and the Web in general that do not fit in to other categories.

Access the HTTP response headers via JavaScript

Postby sproket » Mon Jul 23, 2012 10:15 am

Hi all,

I was wondering if there is any specification or discussion on allowing JavaScript to access the HTTP headers in the response sent from the server.

It seems you can do this by sending a separate head request with XMLHttpRequest, however that means making another trip to the server.

If the server sends back arbitrary HTTP headers there seems to be no way of reading these.

Thoughts?

TIA
sproket
<h6>
 
Posts: 1
Joined: Mon Jul 23, 2012 10:09 am

Re: Access the HTTP response headers via JavaScript

Postby JAB Creations » Tue Jul 24, 2012 3:20 am

Welcome to the forums Tia.

Unfortunately it seems that is not a supported feature after quickly going though the ECMAScript 5.1 specifications...
http://ecma-international.org/ecma-262/5.1/

It seems like at this moment in time you'll have to use XMLHttpRequest like so...

Code: Select all
//var url = 'http://www.example.com/';
var x = new XMLHttpRequest();
x.open('GET', url, false);
x.send(null);
var h = x.getAllResponseHeaders();
alert(h);
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA

Re: Access the HTTP response headers via JavaScript

Postby zcorpan » Thu Aug 16, 2012 7:56 am

sproket wrote:Hi all,

I was wondering if there is any specification or discussion on allowing JavaScript to access the HTTP headers in the response sent from the server.

It seems you can do this by sending a separate head request with XMLHttpRequest, however that means making another trip to the server.
Right, there's no way to do that. The server can repeat the headers in the response body in an HTML comment or as a JSON blob in a <script> block or whatever, though.

sproket wrote:If the server sends back arbitrary HTTP headers there seems to be no way of reading these.

What's the use case?
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest