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

jRi*_*cca 25 html javascript iframe http

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

Wal*_*alu 40

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

<iframe name="hidden_iframe" ...></iframe>
Run Code Online (Sandbox Code Playgroud)


Kis*_*reK 18

如何使用表单的target属性指向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>
Run Code Online (Sandbox Code Playgroud)

  • post.php有什么作用?为什么这有必要? (3认同)

小智 13

只是给出一个具体的工作实例

<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>
Run Code Online (Sandbox Code Playgroud)

  • 使用隐藏表单然后提交是没有意义的。为什么不直接使用 ajax 请求并使用收到的响应填充目标 div? (2认同)