怎样获取响应头: Response Header

1. 使用 xhr.getResponseHeader()可以获取指定响应头字段值.

function getHeaderTime() {
  console.log(this.getResponseHeader("Last-Modified"));
}

var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'yourpage.html');
xhr.onload = getHeaderTime;
xhr.send();

2. 使用 xhr.getAllResponseHeader() 可以获取所有响应字段值

var xhr = new XMLHttpRequest();
xhr.open('GET', 'foo.txt', true);
xhr.send();

xhr.onreadystatechange = function () {
  if (this.readyState === 4) {
    var headers = xhr.getAllResponseHeaders();
  }
}

注意: 

1. 如果没有接收到服务器返回的头信息, 则两个方法都返回null;

2. xhr.getResponseHeader()的参数名不区分大小写;

原文地址:https://www.cnblogs.com/aisowe/p/11558553.html