获取服务页面响应标头

时间:2017-06-13 17:01:51

标签: javascript http header response

我需要从提供的页面请求中检索响应标头值。

所以,假设我打开控制台并转到以下网址 - >

https://www.w3schools.com/tags/tag_header.asp

网络选项卡中的第一个请求是一个GET请求,其中包含对此URL的响应代码(304)。

我需要从此URL获取响应标头参数值。它可以在JavaScript中使用吗?

玩了一下 -

window.fetch(window.location.href).then((res) => console.log(res.headers.get('x-cache')))

这样可行,但是可以从原始请求中获取标头而不是发出新请求吗?

1 个答案:

答案 0 :(得分:0)

window.location对象中,您有一些有趣的参数:

host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "https://stackoverflow.com/questions/44527522/get-the-server-pages-response-header"
origin: "https://stackoverflow.com"
pathname: "/questions/44527522/get-the-server-pages-response-header"

这样你就可以获得网址。

要检索引荐来源,请使用document.referrer

要访问用户代理,请使用navigator.userAgent