如何通过 JavaScript 访问 HTTP 请求标头字段?

问题描述:

我想在客户端 JavaScript 中捕获 HTTP 请求标头字段,主要是 Referer 和 User-Agent.我如何访问它们?

I want to capture the HTTP request header fields, primarily the Referer and User-Agent, within my client-side JavaScript. How may I access them?

Google Analytics 设法通过 JavaScript 获取您嵌入页面中的数据,因此这绝对是可能的.

Google Analytics manages to get the data via JavaScript that they have you embed in you pages, so it is definitely possible.

相关:
在 JavaScript 中访问网页的 HTTP 标头

如果你想访问referrer和user-agent,客户端Javascript可以使用它们,但不能直接访问headers.

If you want to access referrer and user-agent, those are available to client-side Javascript, but not by accessing the headers directly.

要检索引用者,请使用 document.referrer代码>.
要访问用户代理,请使用 navigator.userAgent.

To retrieve the referrer, use document.referrer.
To access the user-agent, use navigator.userAgent.

正如其他人所指出的,HTTP 标头不可用,但您特别询问了通过 Javascript 提供的引用器和用户代理.

As others have indicated, the HTTP headers are not available, but you specifically asked about the referer and user-agent, which are available via Javascript.