通过post方法将数据发送到iframe

问题描述:

我有一个指向moodle站点的iframe。我需要传递给我的用户名和密码,以便在加载iframe时,我会自动登录到moodle。所以,我有这样的事情:通过post方法将数据发送到iframe

<div id="iframe" style="width:787px; height:700px;"> 
<iframe id="iframeCon" src ="http://www.somesite.net/moodle/login/index.php" width="100%" height="100%" frameborder="0"> 

</iframe> 

我的问题是如何使用POST方法到这个URL发送我的用户名和密码?

+1

不使用http没有SSL来做到这一点。通过不受保护的网络发送密码绝不是必需的。 – iankit 2013-06-12 11:40:49

+1

可能重复[你如何发布到iframe?](http://*.com/questions/168455/how-do-you-post-to-an-iframe) – 2014-03-14 10:11:40

要发布到iframe,您必须使用form target

<form id="moodleform" target="iframe" 
     method="post" action="http://www.example.com/login/index.php" > 
    <input type="hidden" name="username" value="guest"/> 
    <input type="hidden" name="password" value="guest"/> 
    <input type="hidden" name="testcookies" value="1"/> 
</form> 
<iframe name="iframe"></iframe> 
<script type="text/javascript"> 
    document.getElementById('moodleform').submit(); 
</script> 
+0

我试过了,但它是不工作。它只是在一个新窗口中打开URL“http://www.example.com/login/index.php”,而我仍然没有登录。当然,我没有登录两个“guest”,而是放置了我的凭证。 – misaizdaleka 2010-09-27 12:00:47

+0

适用于我针对moodle.org。如果它在新窗口中打开,则省略了命名的“iframe”。 – bobince 2010-09-27 12:09:00

+0

看起来很奇怪,我对'name =“toframe”'有个问题。将它改为'name =“frame”'工作。所以也许@misaizdaleka有'name =“iframe”'这样的问题(它可能是一个保留的名字......)。 – Yvan 2017-09-13 11:58:19