只读 redirected 特性为 响应 interface indicates whether or not the response is the result of a request you made which was redirected.

Relying on redirected to filter out redirects makes it easy for a forged redirect to prevent your content from working as expected. Instead, you should actually instead do the filtering when you call fetch() . See the example Disallowing redirects , which shows this being done.

句法

var isRedirected = Response.redirected;
					

A 布尔 which is true if the response indicates that your request was redirected.

范例

Detecting redirects

Checking to see if the response comes from a redirected request is as simple as checking this flag on the 响应 object. In the code below, a textual message is inserted into an element when a redirect occurred during the fetch operation. Note, however, that this isn't as safe as outright rejecting redirects if they're unexpected, as described under Disallowing redirects 下文。

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;
});
					

Disallowing redirects

Because using redirected to manually filter out redirects can allow forgery of redirects, you should instead set the redirect mode to "error" init parameter when calling fetch() ,像这样:

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;
});
					

规范

规范 状态 注释
Fetch
The definition of 'redirected' in that specification.
实时标准 初始定义

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
redirected Chrome 60 Edge 16 Firefox 49 IE No Opera 47 Safari No WebView Android 60 Chrome Android 60 Firefox Android 49 Opera Android 44 Safari iOS No Samsung Internet Android 8.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: