Javascript - 重定向到带有 POST 数据的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16774533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:56:33  来源:igfitidea点击:

Javascript - redirect to a page with POST data

javascriptajaxpostredirect

提问by Yannick Bloem

I've got a page where the user can search (all in AJAX) in the database and select, let's say, different type of data on the page. When's he chosen them, what I need is to redirect the user to ANOTHER page (still on my website though), with POST data. The new page where the user will arrive needs that POST data, and the user needs to go to the new page.

我有一个页面,用户可以在其中在数据库中搜索(全部使用 AJAX)并选择页面上不同类型的数据。当他选择它们时,我需要将用户重定向到另一个页面(尽管仍在我的网站上),并带有 POST 数据。用户将到达的新页面需要该 POST 数据,并且用户需要转到新页面。

I've read that I could create a form before loading the page and simply submit it when I want to redirect, but the thing is that I don't have the data that will be included in the before hand.

我已经读过我可以在加载页面之前创建一个表单,并在我想重定向时简单地提交它,但问题是我没有将包含在之前的数据。

I've tried doing something like this:

我试过做这样的事情:

document.write('<form id="reviseCombi" method="post" action="otherPage.php"><input name="input1" type="hidden" value="ok" /><input type="input2" type="hidden" value="'+ mots +'" /></form>');
f=document.getElementById('reviseCombi');
if(f){
    f.submit();
}

But the second input doesn't arrive to the new page when I check with Tamper Data. And it reloads the actual page first, which is weird too.

但是当我检查篡改数据时,第二个输入没有到达新页面。它首先重新加载实际页面,这也很奇怪。

Is there a better way to do it?

有没有更好的方法来做到这一点?

EDIT: I got an answer for the input part. The page now gets the two inputs. But: it still reloads a blank page ONLY containing the form (and the html, body, etc.) and then it redirects. Any idea how I can have it directly?

编辑:我得到了输入部分的答案。该页面现在获得两个输入。但是:它仍然会重新加载一个仅包含表单(以及 html、正文等)的空白页面,然后重定向。知道如何直接拥有它吗?

Sorry if it's not very clear, it's tough to explain. I'll answer any comments to explain it better. Thanks alot.

对不起,如果不是很清楚,很难解释。我会回答任何评论以更好地解释它。非常感谢。

回答by Sharky

Q: "But the second input doesn't arrive to the new page"

问:“但是第二个输入没有到达新页面”

A: Your second input does NOT have name! (type="input2" make it name="input2")

答:您的第二个输入没有名称!(类型="input2" 使其名称为="input2")

Q: "And it reloads the actual page first, which is weird too."

问:“它首先重新加载实际页面,这也很奇怪。”

A: If you are using firefox and you echo data before the < DOCTYPE > and < HTML > tags, you get an auto refresh. if you wish to debug, echo data inside the < BODY >

答:如果您使用的是 Firefox 并且在 < DOCTYPE > 和 < HTML > 标签之前回显数据,则会自动刷新。如果你想调试,回显<BODY>里面的数据

A2: Its possible that the strange reload happens because you are doing that document.write of the form outside the < BODY > or maybe inside the < HEAD > ... if thats the case why not doing this:

A2:可能发生奇怪的重新加载是因为您正在 < BODY > 外或 < HEAD > 内执行该表单的 document.write ......如果是这样,为什么不这样做:

html

html

<div id="myformcontainer"></div>

js

js

function CreateAFormInsideMyDivAndSubmitIt(mots)
{
    var mydiv = document.getElementById('myformcontainer').innerHTML = '<form id="reviseCombi" method="post" action="otherPage.php"><input name="input1" type="hidden" value="ok" /><input name="input2" type="hidden" value="'+ mots +'" /></form>';
    f=document.getElementById('reviseCombi');
    if(f){
    f.submit();
    }
}

jsfiddle: http://jsfiddle.net/MDx8H/1/(alerts instead of submits)

jsfiddle:http: //jsfiddle.net/MDx8H/1/(警报而不是提交)