Accessing the web page's HTTP Headers in JavaScript

前端 未结 17 2674
日久生厌
日久生厌 2020-11-21 04:53

How do I access a page\'s HTTP response headers via JavaScript?

Related to this question, which was modified to ask about accessing two specific HTTP headers.

<
17条回答
  •  星月不相逢
    2020-11-21 05:33

    Allain Lalonde's link made my day. Just adding some simple working html code here.
    Works with any reasonable browser since ages plus IE9+ and Presto-Opera 12.

    
    (XHR) Show all response headers
    
    

    All Response Headers with XHR

    Note: You get headers of a second request, the result may differ from the initial request.


    Another way
    is the more modern fetch() API
    https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
    Per caniuse.com it's supported by Firefox 40, Chrome 42, Edge 14, Safari 11
    Working example code:

    
    fetch() all Response Headers
    
    

    All Response Headers with fetch()

提交回复
热议问题