---
title: Response.redirect()
slug: Web/API/Response/redirect
translation_of: Web/API/Response/redirect
---
<div>{{APIRef("Fetch")}}</div>

<p>{{domxref("Response")}} 接口的 <strong><code>redirect()</code></strong> 方法返回一个可以重定向到指定 URL 的 <code>Response</code> 。</p>

<div class="note">
<p><strong>Note</strong>: 主要和 <a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a> 有关。 A controlling service worker could intercept a page's request and redirect it as desired. This will actually lead to a real redirect if a service worker sends it upstream.</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var <em>response</em> = Response.redirect(<em>url</em>, <em>status</em>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>url</code></dt>
 <dd>The URL that the new response is to originate from.</dd>
 <dt><code>status</code> {{optional_inline}}</dt>
 <dd>用于 response 的可选的状态码 (e.g., <code>302</code>.)</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>一个 {{domxref("Response")}} 对象。</p>

<h3 id="异常">异常</h3>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">异常类型</th>
   <th scope="col">说明</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>RangeError</code></td>
   <td>status 不是一个重定向的状态码。</td>
  </tr>
  <tr>
   <td><code>TypeError</code></td>
   <td>url 不可用。</td>
  </tr>
 </tbody>
</table>

<h2 id="示例">示例</h2>

<pre class="brush: js">responseObj.redirect('https://www.example.com', 302);</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('Fetch','#dom-response-redirect','redirect()')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Response.redirect")}}</p>

<h2 id="参阅">参阅</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP 访问控制 (CORS)</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>