怎樣獲取響應頭: 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()的參數名不區分大小寫;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM