2010-09-27 72 views
8

我有一個指向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://stackoverflow.com/questions/168455/how-do-you-post-to-an-iframe) – 2014-03-14 10:11:40

回答

22

要發佈到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