Response: thuộc tính ok

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 ok của giao diện Response chứa một giá trị boolean cho biết phản hồi có thành công hay không, tức status nằm trong khoảng 200-299.

Giá trị

Một giá trị boolean.

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>.

Note: Ở đầu khối fetch() chúng ta ghi giá trị ok của phản hồi ra console.

js
const myImage = document.querySelector("img");

const myRequest = new Request("flowers.jpg");

fetch(myRequest).then((response) => {
  console.log(response.ok); // trả về true nếu phản hồi thành công
  response.blob().then((myBlob) => {
    const objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});

Thông số kỹ thuật

Specification
Fetch
# ref-for-dom-response-ok②

Tương thích trình duyệt

Xem thêm