跨域POST的iframe

4

我想把表单中的数据POST到另一个域名下的IFRAME。是否有简单的方法可以实现这一点?

<iframe name="iframe" width="100" height="100" src="www.otherdomain.com" />

<form action="www.mydomain.com" method="post" target="iframe">
<input type="text" name="text1" value="123" />
<input type="text" name="text2" value="456" />
<input type="submit" value="submit"/>
</form>
1个回答

5

我认为您的示例应该可以使用。我在本地主机上设置了两个虚拟主机pastefrom.com和pasteto.com。

http://pastefrom.com/index.html:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>test</title>
</head>
<body>
<iframe name="iframe" id="iframe" src="http://pasteto.com/index.php" width="500" height="500"></iframe>

<form action="http://pasteto.com/index.php" method="post" target="iframe">
    <input type="text" name="search" value="google" />
    <input type="submit" value="submit"/>
</form>

</body>
</html>

http://pasteto.com/index.php:

<pre><?php var_dump($_POST);?></pre>

当提交表单时,它会在pasteto.com上显示帖子数据。

array(1) {
  ["search"]=>
  string(6) "google"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接