获取 XMLHttpRequest 中的 SENT 标头
尝试从 XHR 对象获取请求标头,但没有成功,该对象是否有隐藏的方法或属性会公开浏览器发送的标头?
我已经知道如何设置自定义请求标头并查看响应标头,我希望获取发送的所有请求标头、浏览器创建的标头和我的自定义标头的列表。
我使用的是webkit/chrome,不关心其他浏览器。
编辑:我不想监视请求,我正在构建一个网络应用程序,我需要列出这些标头并在应用程序中显示它们,请不要告诉我有关 fiddler、firebug 和 chrome 工具的信息,这不是什么我在找。
Trying to get the Request headers from the XHR object, but with no luck, is there a hidden method or property of that object that will expose the headers sent by the browser?
I already know how to set custom request headers and view the response headers, I'm looking to get a list of all REQUEST headers sent, ones created by the browser and my custom ones.
I'm using webkit/chrome, don't care about other browsers.
EDIT: I'm not looking to monitor the request, I'm building a web app and I need to list those headers and display them within the app, please don't tell me about fiddler, firebug and chrome tools, that's not what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XMLHttpRequest API 中没有方法获取发送的请求标头。有一些方法可以仅获取响应标头,也可以设置请求标头。
您必须让服务器回显标头,或者使用 Wireshark 等数据包嗅探器。
There is no method in the XMLHttpRequest API to get the sent request headers. There are methods to get the response headers only, and set request headers.
You'll have to either have the server echo the headers, or use a packet sniffer like Wireshark.
尝试使用 Fiddler Web 调试器。
http://www.fiddler2.com/fiddler2/
您可以捕获以任何方式发送的请求浏览器以及检查请求标头、响应标头,甚至复制捕获发送的请求并将其作为您自己的请求发送出去。
Try using Fiddler Web Debugger.
http://www.fiddler2.com/fiddler2/
You can capture the request that was sent in any browser as well as inspect the request headers, response headers, and even copy a capture sent request and send it out as your own.
顾名思义,发送的标头已发送,(废话)!并且 XMLHttpRequest 类不会将发送的标头存储在 RAM 中或将发送的标头放入 XMLHttpRequest 的实例中...这对性能有好处。
如果你想获取已经发送的header,你需要做的就是创建一个日志机制。
由于自定义请求头是通过
XMLHttpRequest.prototype.setRequestHeader()
创建的,因此需要拦截XMLHttpRequest.prototype.setRequestHeader()
;就这样。无需外部库或 Wireshark。全部在 Javascript 内完成;
只需确保上面的拦截代码在任何 XMLHttpRequest 初始化之前执行即可。
诗。这段代码显然只会拦截通过 setRequestHeader() 创建的自定义标头。 XMLHttpRequest 本身会设置一些默认标头,这些标头无法通过此方法访问。
As the name suggests, sent headers were SENT, (duh)! And the XMLHttpRequest class doesn't store sent headers in RAM or put sent headers in an instance of XMLHttpRequest... which is good for performance.
If you want to get the headers that have been sent, all you need to do is to create a log mechanism.
And since custom request header are created through
XMLHttpRequest.prototype.setRequestHeader()
, You need to interceptXMLHttpRequest.prototype.setRequestHeader()
;That's all. No need for external library nor Wireshark. All done within Javascript;
Just make sure the intercept code above executed before any XMLHttpRequest initialization.
Ps. this code will obviously only intercept the custom header created through
setRequestHeader()
. The XMLHttpRequest itself will set some default headers which can't be accessed through this method.假设您正在使用 jQuery,并且您正在寻找附加的任何内容,但可能不是发送的所有标头,这可能会有所帮助。不确定它是否满足您的确切需求(因为浏览器倾向于添加自己的东西),但如果您需要先获取自己的标头,则可以使用以下方法:
Assuming you are using jQuery, and you're looking for anything attached, but maybe not ALL headers sent, this could help. Not sure if it meets your exact needs, (since the browser tends to add its own things), but if you need to grab your own headers first, this works: