简体   繁体   English

在 XMLHttpRequest 中获取 SENT 标头

[英]get SENT headers in an XMLHttpRequest

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?尝试从 XHR object 获取请求标头,但没有运气,是否存在该 object 的隐藏方法或属性将公开浏览器发送的标头?

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.我正在使用 webkit/chrome,不关心其他浏览器。

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 应用程序,我需要列出这些标题并在应用程序中显示它们,请不要告诉我有关提琴手、萤火虫和 chrome 工具的信息,这不是什么我在找。

There is no method in the XMLHttpRequest API to get the sent request headers . XMLHttpRequest API中没有方法来获取发送的请求标头 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. 您必须让服务器回显标头,或使用像Wireshark这样的数据包嗅探器。

Try using Fiddler Web Debugger. 尝试使用Fiddler Web Debugger。

http://www.fiddler2.com/fiddler2/ 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. 您可以捕获在任何浏览器中发送的请求,也可以检查请求标头,响应标头,甚至复制捕获发送的请求并将其作为您自己的请求发送出去。

As the name suggests, sent headers were SENT, (duh).顾名思义,发送的标头是 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.并且 XMLHttpRequest class 不会将发送的标头存储在 RAM 中或将发送的标头放在 XMLHttpRequest 的实例中......这对性能有好处。

If you want to get the headers that have been sent, all you need to do is to create a log mechanism.如果你想获取已经发送的headers,你需要做的就是创建一个日志机制。

And since custom request header are created through XMLHttpRequest.prototype.setRequestHeader() , You need to intercept XMLHttpRequest.prototype.setRequestHeader() ;并且由于自定义请求 header 是通过XMLHttpRequest.prototype.setRequestHeader()创建的,所以需要拦截XMLHttpRequest.prototype.setRequestHeader()

var sentHeaders = {}
var originalXMLHttpRequest_setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;

XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
    sentHeaders[header] = value;
    originalXMLHttpRequest_setRequestHeader.call(this, header, value);
}

That's all.就这样。 No need for external library nor Wireshark.不需要外部库或 Wireshark。 All done within Javascript;全部在 Javascript 内完成;

Just make sure the intercept code above executed before any XMLHttpRequest initialization.只要确保上面的拦截代码在任何 XMLHttpRequest 初始化之前执行。

Ps.附言。 this code will obviously only intercept the custom header created through setRequestHeader() .这段代码显然只会拦截通过setRequestHeader()创建的自定义 header 。 The XMLHttpRequest itself will set some default headers which can't be accessed through this method. XMLHttpRequest 本身会设置一些无法通过此方法访问的默认标头。

Assuming you are using jQuery, and you're looking for anything attached, but maybe not ALL headers sent, this could help. 假设您正在使用jQuery,并且您正在寻找附加的任何内容,但可能没有发送所有标头,这可能有所帮助。 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: 不确定它是否满足您的确切需求,(因为浏览器倾向于添加自己的东西),但如果您需要首先获取自己的标题,这可以工作:

$.ajaxSetup({
    beforeSend: function (jqXHR, settings) {
        if(!(settings.headers && settings.headers.token)) {
            //no token header was set, so set the request header
            jqXHR.setRequestHeader('token', newtoken);
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM