Javascript 如何使用 HTTP POST 请求将参数发送到 Iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6730522/
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-08-23 22:57:22  来源:igfitidea点击:

How to send parameter to Iframe with a HTTP POST request

javascripthtmlhttpiframe

提问by jRicca

I have to send some parameter to an IFRAME with POST method. I have read here Setting the HTTP request type of an <iframe>that it isn't possible. I'm thinking a solution in Javascript but I can't implement it so I can't test if it is a valid solution for this issue. I want to ask if someone has the same problem and if it is possible to solve and in positive case how to?

我必须使用 POST 方法将一些参数发送到 IFRAME。我在这里阅读了设置 <iframe> 的 HTTP 请求类型,这是不可能的。我正在考虑 Javascript 中的解决方案,但我无法实现它,因此我无法测试它是否是解决此问题的有效解决方案。我想问一下是否有人有同样的问题,是否可以解决,在积极的情况下如何解决?

回答by Walialu

<form ... target="hidden_iframe">
...
</form>

<iframe name="hidden_iframe" ...></iframe>

回答by KishoreK

How about using the target attribute of the form to point to iFrame?

如何使用表单的目标属性指向 iFrame?

 <form target="myIframe" action="http://localhost/post.php" method="post">
    <input type="hidden" value="someval" />
    <input type="submit">
</form>

<iFrame src="" name="myIframe"></iFrame>

回答by user3420157

Just to give a concrete working example

只是举一个具体的工作例子

<form id="loginForm" target="myFrame"  action="https://localhost/j_spring_security_check" method="POST">
 <input type="text" name="j_username" value="login" />
 <input type="text" name="j_password" value="password" />
 <input type="submit">
</form>

<iframe name="myFrame" src="#">
   Your browser does not support inline frames.
</iframe>

// Hide the form and do the submit 
<script>
 $(document).ready(function(){
 var loginform= document.getElementById("loginForm");
 loginform.style.display = "none";
 loginform.submit();
});
</script>