Response: thuộc tính status
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 status của giao diện Response chứa các HTTP status codes của phản hồi.
Ví dụ: 200 cho thành công, 404 nếu tài nguyên không tìm thấy.
Giá trị
Một số nguyên không dấu. Đây là một trong các HTTP response status codes.
Giá trị 0 được trả về cho phản hồi có type là opaque, opaqueredirect hoặc error.
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 <img>.
Lưu ý rằng ở đầu khối fetch() chúng ta ghi giá trị status của phản hồi ra console.
const myImage = document.querySelector("img");
const myRequest = new Request("flowers.jpg");
fetch(myRequest)
.then((response) => {
console.log("response.status =", response.status); // response.status = 200
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-status①> |