利用XMLHttpRequest.getAllResponseHeaders() 方法返回所有的响应头
let req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
let headers = req.getAllResponseHeaders().toLowerCase();
console.log(headers);
// 由于返回的是用\r\n来进行分割的字符串,需要做转换
let arr = headers.trim().split(/[\r\n]+/);
let headerMap = {};
arr.forEach(function (line) {
let parts = line.split(': ')
let header = parts.shift()
let value = parts.join(': ')
headerMap[header] = value
})
let contentType = headerMap['content-type'];
console.log('contentType: ', content-type);