XMLHttpRequest.getResponseHeader() - Web APIs 编辑

The XMLHttpRequest method 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.

Note: 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.

Syntax

var myHeader = XMLHttpRequest.getResponseHeader(headerName);

Parameters

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

Return value

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.

Example

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();
    }
  }
}

Specifications

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

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:120 次

字数:4376

最后编辑:6 年前

编辑次数:0 次

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