javascript 在 XMLHttpRequest 中获取 SENT 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7564007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:33:07  来源:igfitidea点击:

get SENT headers in an XMLHttpRequest

javascriptgoogle-chromewebkitxmlhttprequest

提问by Ahmad Nassri

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 对象获取请求标头,但没有运气,该对象是否有隐藏的方法或属性会暴露浏览器发送的标头?

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.

编辑:我不打算监视请求,我正在构建一个网络应用程序,我需要列出这些标题并在应用程序中显示它们,请不要告诉我有关提琴手、萤火虫和 chrome 工具的信息,这不是什么我在找。

采纳答案by AmGates

There is no method in the XMLHttpRequest APIto get the sent request headers. There are methods to get the response headers only, and setrequest headers.

XMLHttpRequest API 中没有方法来获取发送的请求标头。有一些方法可以只获取响应头,并设置请求头。

You'll have to either have the server echo the headers, or use a packet sniffer like Wireshark.

您必须让服务器回显标头,或者使用像 Wireshark 这样的数据包嗅探器。

回答by Kenneth

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.

您可以捕获在任何浏览器中发送的请求,并检查请求标头、响应标头,甚至复制捕获发送的请求并将其作为您自己的请求发送出去。

回答by mix3d

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:

假设您正在使用 jQuery,并且您正在寻找任何附加的东西,但可能不是所有的标头发送,这可能会有所帮助。不确定它是否满足您的确切需求(因为浏览器往往会添加自己的东西),但是如果您需要先获取自己的标题,则可以这样做:

$.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);
        }
    }
})