302 重定向以后,前端获取重定向的地址并跳转
在前端中,可以通过发送请求并检查响应头来获取重定向的地址,并根据该地址进行跳转。以下是一个使用 fetch
函数获取重定向地址并进行跳转的示例:
fetch('https://example.com/redirect', { redirect: 'manual' }) .then(response => { if (response.status === 302) { const redirectUrl = response.headers.get('Location'); window.location.href = redirectUrl; } else { // 处理其他状态码 } }) .catch(error => { console.error('Error fetching redirect URL:', error); });
.