如何通过 JavaScript 访问 HTTP 请求标头字段? [英] How do I access the HTTP request header fields via JavaScript?

查看:36
本文介绍了如何通过 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.

这篇关于如何通过 JavaScript 访问 HTTP 请求标头字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆