get SENT headers in an XMLHttpRequest

后端 未结 3 1891
日久生厌
日久生厌 2020-12-15 04:10

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

相关标签:
3条回答
  • 2020-12-15 04:25

    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.

    0 讨论(0)
  • 2020-12-15 04:39

    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:

    $.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);
            }
        }
    })
    
    0 讨论(0)
  • 2020-12-15 04:40

    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.

    0 讨论(0)
提交回复
热议问题