Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. XMLHttpRequest
  4. getAllResponseHeaders()

XMLHttpRequest: getAllResponseHeaders() method

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⁩.

Note: This feature is available inWeb Workers, except forService Workers.

TheXMLHttpRequest methodgetAllResponseHeaders() returns all the responseheaders, separated byCRLF, as a string, or returnsnullif no response has been received.

If a network error happened, an empty stringis returned.

Note:For multipart requests, this returns the headers from thecurrent part of the request, not from the original channel.

Syntax

js
getAllResponseHeaders()

Parameters

None.

Return value

A string representing all of the response's headers (except thosewhose field name isSet-Cookie) separated byCRLF,ornull if no response has been received. If a network errorhappened, an empty string is returned.

An example of what a raw header string looks like:

http
date: Fri, 08 Dec 2017 21:04:30 GMT\r\ncontent-encoding: gzip\r\nx-content-type-options: nosniff\r\nserver: meinheld/0.6.1\r\nx-frame-options: DENY\r\ncontent-type: text/html; charset=utf-8\r\nconnection: keep-alive\r\nstrict-transport-security: max-age=63072000\r\nvary: Cookie, Accept-Encoding\r\ncontent-length: 6502\r\nx-xss-protection: 1; mode=block\r\n

Each line is terminated by both carriage return and line feed characters(\r\n). These are essentially delimiters separating each of the headers.

Note:In modern browsers, the header names are returned in all lowercase, as per the latest spec.

Examples

This example examines the headers in the request'sreadystatechange event. The code shows how to obtainthe raw header string, as well as how to convert it into an array of individual headersand then how to take that array and create a mapping of header names to their values.

js
const request = new XMLHttpRequest();request.open("GET", "foo.txt", true);request.send();request.onreadystatechange = () => {  if (request.readyState === request.HEADERS_RECEIVED) {    // Get the raw header string    const headers = request.getAllResponseHeaders();    // Convert the header string into an array    // of individual headers    const arr = headers.trim().split(/[\r\n]+/);    // Create a map of header names to values    const headerMap = {};    arr.forEach((line) => {      const parts = line.split(": ");      const header = parts.shift();      const value = parts.join(": ");      headerMap[header] = value;    });  }};

Once this is done, you can, for example:

js
const contentType = headerMap["content-type"];

This obtains the value of theContent-Type header into the variablecontentType.

Specifications

Specification
XMLHttpRequest
# the-getallresponseheaders()-method

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp