提交其他域名中iframe的POST表单

时间:2017-03-01 04:13:12

标签: javascript jquery html iframe

假设website1.com/page_1.php的表单如下:

<form action="http://www.website1.com/action" method="post" id="myForm">
   <input type="hidden" name="_token" value="sdfddgfg">
   <input type="hidden" name="category_id" value="1">
   <input type="submit" class="btn send" value="Send">
</form>

并且website2.com/page_2.php有这样的iframe:

<iframe src="http://www.website1.com/page_1.php"></iframe>

使用JQuery或JS,如何在访问myForm时无需点击即可提交ID为website2.com/page_2.php?的表单。

1 个答案:

答案 0 :(得分:0)

您可以仅在iframe的HTML中执行类似的操作(您无需在“父”窗口中执行任何操作:

<html>

<head>
</head>

<body>
  <form action="https://www.yoursite.com/" method="post" id="myForm">
    <input type="hidden" name="_token" value="sdfddgfg">
    <input type="hidden" name="category_id" value="1">
    <input type="submit" class="btn send" value="Send">
  </form>
  <script>
    (function() {
      document.getElementById('myForm').submit()
    })();
  </script>
</body>

</html>