admin管理员组文章数量:1316382
Is it possible to gather the HTTP headers in JavaScript ? This is just a thought of mine after using Firebug for days. In one of the posts I came to know that, it is impossible to find the HTTP headers in JavaScript, whereas in firebug, we can see the response headers (client side)
so my question is can we cache the HTTP headers in JavaScript ?
Is it possible to gather the HTTP headers in JavaScript ? This is just a thought of mine after using Firebug for days. In one of the posts I came to know that, it is impossible to find the HTTP headers in JavaScript, whereas in firebug, we can see the response headers (client side)
so my question is can we cache the HTTP headers in JavaScript ?
Share Improve this question edited Apr 7, 2015 at 10:02 asked Jul 9, 2010 at 18:47 user372551user372551 1- Related question: stackoverflow./questions/220231/… – Brock Adams Commented Jun 22, 2011 at 19:00
2 Answers
Reset to default 5The HTTP headers aren't available in JavaScript.
However you could use XMLHttpRequest
to do a HEAD
request to any resource within the same domain:
var xhr = new XMLHttpRequest();
xhr.open('HEAD', '/', true); // Relative path of resource
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.getAllResponseHeaders());
}
}
xhr.send(null);
The above will return something like this (running it in Firebug on this page):
Server: nginx
Date: Fri, 09 Jul 2010 18:58:30 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-Control: public, max-age=60
Content-Length: 33273
Content-Encoding: gzip
Expires: Fri, 09 Jul 2010 18:59:31 GMT
Last-Modified: Fri, 09 Jul 2010 18:58:31 GMT
Vary: *
You can easily get the value of single headers like this:
xhr.getResponseHeader('Last-Modified');
Firebug is not a web application - it is a XUL application (that is, a Mozilla application, written with XUL and javascript) and as such has access to http headers that browser side javascript cannot access.
You cannot access http header via javascript in the browser.
本文标签: Http headers in JavascriptStack Overflow
版权声明:本文标题:Http headers in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003311a2411455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论