ส่วนหัวของ HTTP Response ใน AJAX ใน JavaScript
ส่วนหัวของ response ถูกเก็บอยู่ใน
คุณสมบัติ headers ของออบเจ็กต์ response
มาลองรับส่วนหัวบางส่วนกัน:
button.addEventListener('click', function() {
fetch('/ajax.html').then(response => {
console.log(response.headers.get('Content-Type'));
});
});
ทีนี้มาลองวนลูปเพื่ออ่านส่วนหัวทั้งหมด:
button.addEventListener('click', function() {
fetch('/ajax.html').then(response => {
for (let [key, value] of response.headers) {
console.log(key, value);
}
});
});
รับเนื้อหาของส่วนหัว
Content-Language
รับเนื้อหาของส่วนหัว
Content-Length