Response: thuộc tính headers
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
Note: This feature is available in Web Workers.
Thuộc tính chỉ đọc headers của giao diện Response chứa đối tượng Headers gắn với phản hồi.
Giá trị
Một đối tượng Headers.
Ví dụ
Trong ví dụ Fetch Response của chúng tôi (chạy trực tiếp), chúng ta tạo một đối tượng Request mới bằng constructor Request(), truyền vào một đường dẫn JPG.
Sau đó chúng ta fetch yêu cầu này bằng fetch(), trích blob từ phản hồi bằng Response.blob, tạo object URL từ nó bằng URL.createObjectURL(), rồi hiển thị nó trong phần tử <img>.
Lưu ý rằng ở đầu khối fetch(), chúng ta ghi response headers ra console.
js
const myImage = document.querySelector("img");
const myRequest = new Request("flowers.jpg");
fetch(myRequest)
.then((response) => {
console.log("response.headers =", response.headers);
return response.blob();
})
.then((myBlob) => {
const objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
Thông số kỹ thuật
| Specification |
|---|
| Fetch> # ref-for-dom-response-headers①> |