Response: thuộc tính url

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 url của giao diện Response chứa URL của phản hồi. Giá trị của thuộc tính url sẽ là URL cuối cùng thu được sau mọi chuyển hướng.

Giá trị

Một chuỗi.

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 URL 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.url =", response.url); // response.url = https://mdn.github.io/dom-examples/fetch/fetch-response/flowers.jpg
    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-url①

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

Xem thêm