我如何提交HTML/JS表单到URL 1,但重定向到URL 2



我想使用HTML或JavaScript创建一个表单,提交时将数据发送到一个URL,但将浏览器重定向到另一个。因此,用户在表单中输入数据,然后单击"Submit"。然后将表单数据发送到https://1111.example.com。提交数据后,浏览器被重定向到https://2222.example.com。(我没有控制https://1111.example.com,所以我不能把重定向到那里。)

我如何做到这与HTML或JavaScript与这种类型的形式?:

<form action="https://1111.example.com" method="get">
<p><label for="a">a</label><input type="text" name="a" id="a" /></p>
<p><label for="b">b</label><input type="text" name="b" id="b" /><
<p><input type="submit" id="submit" class="button" value="Submit" /></p>
</form>

我还没能找到解决方案,所以我很感谢你的建议。谢谢!

您必须等待URL被获取,然后根据响应重定向用户。

function sleep(lf_ms) {
return new Promise(resolve => setTimeout(resolve, lf_ms));
}
async function check_form() {
console.log('SENDING');
await sleep(1000);
console.log('CHECKING RESPONSE');
try {
let response = await fetch('http://1111.example.com');
if (response.ok) {
console.log('RESPONSE OK');
window.location = 'http://2222.example.com';
} else {
console.error("HTTP-Error: " + response.status);
}
} catch(e) {
console.error('IMPOSSIBLE REQUEST:');
console.error(e);
}
}
<form onsubmit="check_form(); return false;">
<p><label for="a">a</label><input type="text" name="a" id="a" /></p>
<p><label for="b">b</label><input type="text" name="b" id="b" />
<p><input type="submit" id="submit" class="button" value="Submit" /></p>
</form>

最新更新