Accessing the web page's HTTP Headers in JavaScript

前端 未结 17 2649
日久生厌
日久生厌 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:50

    It's not possible to read the current headers. You could make another request to the same URL and read its headers, but there is no guarantee that the headers are exactly equal to the current.


    Use the following JavaScript code to get all the HTTP headers by performing a get request:

    var req = new XMLHttpRequest();
    req.open('GET', document.location, false);
    req.send(null);
    var headers = req.getAllResponseHeaders().toLowerCase();
    alert(headers);
    

提交回复
热议问题