Accessing the web page's HTTP Headers in JavaScript

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

    This is my script to get all the response headers:

    var url = "< URL >";
    
    var req = new XMLHttpRequest();
    req.open('HEAD', url, false);
    req.send(null);
    var headers = req.getAllResponseHeaders();
    
    //Show alert with response headers.
    alert(headers);
    

    Having as a result the response headers.

    This is a comparison test using Hurl.it:

提交回复
热议问题