「Ajax」getAllResponseHeaders() メソッドでレスポンスヘッダーを取得する

環境
Google Chrome 103.0.5060.134

構文
1.getAllResponseHeaders関数について
XMLHttpRequest の getAllResponseHeaders() メソッドは、すべてのレスポンスヘッダーを CRLF で区切った文字列として返し、
レスポンスを受信していない場合は null を返します。
2.readyStateの値について
値4 データを取得して通信が終了している状態 通信完了
3.statusの値について
値200 特に問題なく通信が成功した状態 成功
4.open(“GET",URLを記述,true);

使用例

<html><!DOCTYPE html>
<html>
<head>
<script>
function xmlFunc(url)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
    // ブラウザIE7+, Firefox, Chrome, Opera, Safari適用
    xmlhttp=new XMLHttpRequest();
  }
else
  {// IE6, IE5以下適用
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById('pA').innerHTML=xmlhttp.getAllResponseHeaders();
    }
  }
   xmlhttp.open("GET",url,true);
   xmlhttp.send();
}
</script>
</head>
<body>

<p id="pA">The getAllResponseHeaders()関数は長さ、サーバータイプ、コンテンツタイプ、最終更新日等、リソースのヘッダー情報を返します。 </p>
<button onclick="xmlFunc('/test/info/resinfo.txt')">ヘッダー情報</button>

</body>
</html>

結果
「ヘッダー情報」を押下すると、以下のメッセージが表示されます。

accept-ranges: bytes content-length: 208 content-type: text/plain date: 
Sun, 14 Aug 2022 07:35:44 GMT etag: "5ad44697-d0" last-modified: Mon, 
16 Apr 2018 06:45:43 GMT x-cache: HIT from BC4_JP-tokyo-tokyo-9-cache-2(baishan)
 x-ser: BC87_dx-lt-yd-shandong-jinan-5-cache-6, BC107_US-DistColumbia-washingtonDC-1-cache-1, 
 BC226_US-California-santa-clara-1-cache-1, BC8_JP-tokyo-tokyo-9-cache-3, BC4_JP-tokyo-tokyo-9-cache-2

 

Ajax

Posted by arkgame