redirect() 方法在 响应 interface returns a 响应 resulting in a redirect to the specified URL.

注意 : This is mainly relevant to the ServiceWorker API . 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.

句法

var response = Response.redirect(url, status);
					

参数

url

The URL that the new response is to originate from.

status 可选
An optional status code for the response (e.g., 302 )。

返回值

A 响应 对象。

异常

异常 解释
RangeError The specified status is not a redirect status.
TypeError The specified URL is invalid.

范例

responseObj.redirect('https://www.example.com', 302);
					

规范

规范 状态 注释
Fetch
The definition of 'redirect()' 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
redirect Chrome Yes Edge 16 Firefox Yes IE No Opera Yes Safari Yes WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

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

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

另请参阅

元数据

  • 最后修改: