XMLHttpRequest 方法 getAllResponseHeaders() returns all the response headers, separated by CRLF , as a string, or returns null 若未收到响应。 If a network error happened, an empty string is returned.

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

句法

var headers = XMLHttpRequest.getAllResponseHeaders();
					

参数

None.

返回值

A ByteString representing all of the response's headers (except those whose field name is Set-Cookie or Set-Cookie2 ) separated by CRLF ,或 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.

注意 : In modern browsers, the header names are returned in all lower case, as per the latest spec.

范例

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 .

规范

规范 状态 注释
XMLHttpRequest
The definition of 'getAllResponseHeaders()' in that specification.
实时标准 WHATWG (Web 超文本应用程序技术工作组) 实时标准

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getAllResponseHeaders Chrome 1 Edge 12 Firefox 4 注意事项
4 注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string 被设为 true ,默认为 false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
IE 5 Opera Yes Safari 1.2 WebView Android Yes Chrome Android Yes Firefox Android 4 注意事项
4 注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string 被设为 true ,默认为 false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
Header names returned in all lower case Chrome Yes Edge 79 Firefox 64 IE 不支持 No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 64 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: