Response
接口中只读的 redirected
属性表明该响应是否为一个重定向的请求的结果.
语法
var isRedirected = Response.redirected;
返回值
一个布尔值 (Boolean
), 如果响应来自重定向的请求, 那么将返回 true
.
示例
检测重定向
检测某个响应是否来自一个重定向的请求就如同检测 Response
对象中这个标识一样容易. 在下面的代码中, 当 fetch 操作引起了重定向, 一段文本信息会被插入到元素中. 但需要注意的是, 这不像下面 禁用重定向 所描述的当重定向不合法时全部阻止的行为一样安全.
fetch("awesome-picture.jpg").then(function(response) {
let elem = document.getElementById("warning-message-box");
if (response.redirected) {
elem.innerHTML = "Unexpected redirect";
} else {
elem.innerHTML = "";
}
return response.blob();
}).then(function(imageBlob) {
let imgObjectURL = URL.createObjectURL(imageBlob);
document.getElementById("img-element-id").src = imgObjectURL;
});
禁用重定向
由于使用 redirected
过滤重定向会允许虚假的重定向, 你应该像下面的例子这样, 当调用 fetch()
时在 init
参数中设置重定向模式为 "error"
:
fetch("awesome-picture.jpg", { redirect: "error" }).then(function(response) {
return response.blob();
}).then(function(imageBlob) {
let imgObjectURL = URL.createObjectURL(imageBlob);
document.getElementById("img-element-id").src = imgObjectURL;
});
规范
Specification | Status | Comment |
---|---|---|
Fetch redirected |
Living Standard | 初始化定义 |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.