Javascript中的Http标头?

编程入门 行业动态 更新时间:2024-10-26 06:31:53
本文介绍了Javascript中的Http标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在JavaScript中收集HTTP标头? 在使用Firebug几天后,这只是我的想法。 在我发现的其中一篇文章中,无法在JavaScript中找到HTTP标头,而在firebug中,我们可以看到响应标头(客户端)

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)

所以我的问题是我们可以在JavaScript中缓存HTTP标头吗?

so my question is can we cache the HTTP headers in JavaScript ?

推荐答案

HTTP标头不可用在JavaScript中。

The HTTP headers aren't available in JavaScript.

但是你可以用 XMLHttpRequest 来做一个 HEAD 请求 同一个域:

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);

以上内容将返回此类内容(在本页的Firebug中运行):

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');

更多推荐

Javascript中的Http标头?

本文发布于:2023-11-10 10:37:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript   Http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!