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

时间:2011-07-18 08:51:17

标签: javascript html http iframe

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

4 个答案:

答案 0 :(得分:38)

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

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

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

建议的解决方案在我的情况下几乎可以正常工作,问题是我提交的表单返回了一个授权 cookie。呈现 iframe 的网站位于与 iframe 网站不同的域中,此方法返回一个错误,提示我无法设置 cookie。有没有安全的方法来解决这个问题?

enter image description here