XMLHttpRequest: getResponseHeader() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

XMLHttpRequestgetResponseHeader() メソッドは、特定のヘッダー値のテキストを含んだ文字列を返します。

同じ名前で複数のレスポンスヘッダーがあった場合、値はカンマと空白で区切って値を接続した単一の文字列として返されます。 getResponseHeader() メソッドは値を UTF バイト列として返します。

メモ: ヘッダー名の検索は、大文字小文字の区別がありません。

ヘッダーすべての生の文字列を取得する必要がある場合は、生のヘッダー文字列全体を返す getAllResponseHeaders() メソッドを使用してください。

構文

js
getResponseHeader(headerName)

引数

headerName

文字列で、テキスト値を取得したいヘッダーの名前を示します。

返値

ヘッダーのテキスト値を表す文字列、または、レスポンスがまだ受信されていないか、そのヘッダーがレスポンスに存在しなければ null です。

この例では、リクエストが生成されて送信され、そして readystatechange ハンドラーを設定してヘッダーが純真で来たことを示す readyState を監視します。その時が来たら、 Content-Type ヘッダーの値を読み取ります。 Content-Type が求められる値でない場合、 XMLHttpRequestabort() を呼び出してキャンセルします。

js
const client = new XMLHttpRequest();
client.open("GET", "unicorns-are-awesome.txt", true);
client.send();

client.onreadystatechange = () => {
  if (client.readyState === client.HEADERS_RECEIVED) {
    const contentType = client.getResponseHeader("Content-Type");
    if (contentType !== my_expected_type) {
      client.abort();
    }
  }
};

仕様書

Specification
XMLHttpRequest
# dom-xmlhttprequest-getresponseheader

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getResponseHeader

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

関連情報