gpt4 book ai didi

javascript - 通过 POST 重定向 - Firefox 中没有任何反应

转载 作者:行者123 更新时间:2023-12-02 14:17:37 25 4
gpt4 key购买 nike

我有一个端点需要 POST 请求并生成一个文件。在前端,我需要使用表单提交来重定向页面。但是,我需要动态生成表单(由于前端的各种原因)。

这是我的尝试(请记住,我必须编辑大量代码,因为它位于各种 http 相关组件中):

sendRedirect(options) {
options.form = this.createForm(options.body);
options.form.setAttribute('method', 'post');
options.form.setAttribute('action', redirectTo);
options.form.submit();
}

createForm(body) {
let form = window.document.createElement('form');

return Object.keys(body).reduce((_form, key) => {
let i = document.createElement('input');
i.setAttribute('type', 'hidden');
i.setAttribute('name', key);
i.setAttribute('value', body[key]);
_form.appendChild(i);
return _form;
}, form);
}

以上内容在 google chrome 中运行良好。然而,在 Firefox 中,什么也没有发生(即 form.submit() 执行后没有发送网络请求)。我已经在 Firefox 中单步执行了代码,它似乎执行得很好(即所有变量都设置为它们应该的值)。代码在 form.submit() 处到达执行结束,然后......什么也没有。

提前致谢。

编辑:感谢昆汀的回答。这是我添加的使其工作的代码:

options.form.style.visibility = 'hidden';
window.document.getElementsByTagName('body')[0].appendChild(options.form);

最佳答案

该表单需要成为文档的一部分,以便 Firefox 提交。

在调用 submit 之前将其附加到(例如)document.body

关于javascript - 通过 POST 重定向 - Firefox 中没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38901210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com