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()的參數名不區分大小寫;