Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Response.redirected

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The read-only redirected property of the Response 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.

Syntax

var isRedirected = Response.redirected;

Value

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

Examples

Detecting redirects

Checking to see if the response comes from a redirected request is as simple as checking this flag on the Response 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 below.

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" in the init parameter when calling fetch(), like this:

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

Specifications

Specification Status Comment
Fetch
The definition of 'redirected' in that specification.
Living Standard Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support ? 49 (49)
 
No support

?

No support
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support 49.0 (49) ? No support ? ? ?

See also

文档标签和贡献者

 此页面的贡献者: Sheppy
 最后编辑者: Sheppy,