Response:url 属性
Response
接口的只读属性 url
包含了响应的 URL 地址。url
属性值为经过重定向后最终获得的 URL 地址。
值
一个字符串。
示例
在 Fetch Response 示例 中(请参见 Fetch Response 实时演示),我们使用 Request()
构造函数创建了一个新的 Request
对象,并向其传递了 JPG 的路径。然后,我们使用 fetch()
获取该请求,使用 Response.blob
从响应中提取一个 blob,使用 URL.createObjectURL
从中创建一个对象 URL,并将其显示在 <img>
中。
请注意,在 fetch()
块的顶部,我们会将响应 URL
记录到控制台。
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;
});
规范
Specification |
---|
Fetch Standard # ref-for-dom-response-url① |
浏览器兼容性
BCD tables only load in the browser