XMLHttpRequest.getAllResponseHeaders() - Web APIs 编辑

The XMLHttpRequest method getAllResponseHeaders() returns all the response headers, separated by CRLF, as a string, or returns null if no response has been received. If a network error happened, an empty string is returned.

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

Syntax

var headers = XMLHttpRequest.getAllResponseHeaders();

Parameters

None.

Return value

A ByteString representing all of the response's headers (except those whose field name is Set-Cookie or Set-Cookie2) separated by CRLF, or null if no response has been received. If a network error happened, an empty string is returned.

An example of what a raw header string looks like:

date: Fri, 08 Dec 2017 21:04:30 GMT\r\n
content-encoding: gzip\r\n
x-content-type-options: nosniff\r\n
server: meinheld/0.6.1\r\n
x-frame-options: DENY\r\n
content-type: text/html; charset=utf-8\r\n
connection: keep-alive\r\n
strict-transport-security: max-age=63072000\r\n
vary: Cookie, Accept-Encoding\r\n
content-length: 6502\r\n
x-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 lower case, as per the latest spec.

Example

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

var request = new XMLHttpRequest();
request.open("GET", "foo.txt", true);
request.send();

request.onreadystatechange = function() {
  if(this.readyState == this.HEADERS_RECEIVED) {

    // Get the raw header string
    var headers = request.getAllResponseHeaders();

    // Convert the header string into an array
    // of individual headers
    var arr = headers.trim().split(/[\r\n]+/);

    // Create a map of header names to values
    var headerMap = {};
    arr.forEach(function (line) {
      var parts = line.split(': ');
      var header = parts.shift();
      var value = parts.join(': ');
      headerMap[header] = value;
    });
  }
}

Once this is done, you can, for example:

var contentType = headerMap["content-type"];

This obtains the value of the Content-Type header into the variable contentType.

Specifications

SpecificationStatusComment
XMLHttpRequest
The definition of 'getAllResponseHeaders()' in that specification.
Living StandardWHATWG living standard

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:92 次

字数:4902

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文