HTTP重定向通常使用GET方法,这是由HTTP协议和浏览器行为决定的。当服务器返回3xx状态码(如301, 302, 307等)时,浏览器会自动使用GET方法发起新的请求,即使原始请求是POST。
HTTP 307状态码表示临时重定向,且要求客户端保持原始请求方法不变。
HTTP/1.1 307 Temporary Redirect
Location: /new-location
在服务器端渲染一个包含所有POST数据的表单,并通过JavaScript自动提交:
<form id="redirectForm" method="post" action="/new-location">
<input type="hidden" name="key1" value="value1">
<input type="hidden" name="key2" value="value2">
</form>
<script>document.getElementById('redirectForm').submit();</script>
// 原始请求前保存数据
localStorage.setItem('postRedirectData', JSON.stringify(postData));
// 重定向后检查并恢复数据
window.addEventListener('DOMContentLoaded', () => {
const savedData = localStorage.getItem('postRedirectData');
if(savedData) {
const postData = JSON.parse(savedData);
// 使用数据...
localStorage.removeItem('postRedirectData');
}
});
const express = require('express');
const app = express();
app.post('/original', (req, res) => {
// 处理原始POST请求
// ...
// 307重定向
res.status(307).location('/new-location').send();
});
app.post('/new-location', (req, res) => {
// 处理重定向后的POST请求
res.send('Received POST after redirect');
});
app.listen(3000);
总结来说,虽然标准的重定向行为是转换为GET,但通过307状态码或前端技术可以实现POST方法的重定向效果。选择哪种方案取决于您的具体需求和环境限制。
没有搜到相关的沙龙