XMLHttpRequest 方法 getResponseHeader() returns the string containing the text of a particular header's value. If there are multiple response headers with the same name, then their values are returned as a single concatenated string, where each value is separated from the previous one by a pair of comma and space. The getResponseHeader() method returns the value as a UTF byte sequence.

注意: The search for the header name is case-insensitive.

If you need to get the raw string of all of the headers, use the getAllResponseHeaders() method, which returns the entire raw header string.

句法

var myHeader = XMLHttpRequest.getResponseHeader(headerName);
					

参数

headerName
ByteString indicating the name of the header you want to return the text value of.

返回值

A ByteString representing the header's text value, or null if either the response has not yet been received or the header doesn't exist in the response.

范例

In this example, a request is created and sent, and a readystatechange handler is established to look for the readyState to indicate that the headers have been received; when that is the case, the value of the Content-Type header is fetched. If the Content-Type isn't the desired value, the XMLHttpRequest is canceled by calling abort() .

var client = new XMLHttpRequest();
client.open("GET", "unicorns-are-teh-awesome.txt", true);
client.send();
client.onreadystatechange = function() {
  if(this.readyState == this.HEADERS_RECEIVED) {
    var contentType = client.getResponseHeader("Content-Type");
    if (contentType != my_expected_type) {
      client.abort();
    }
  }
}
					

规范

规范 状态 注释
XMLHttpRequest
The definition of 'getResponseHeader()' 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
getResponseHeader Chrome 1 Edge 12 Firefox 1 注意事项
1 注意事项
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 8 Safari 1.2 WebView Android 1 Chrome Android 18 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 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: