因此,我的应用程序页面(不同的域)中有一个iframe,其中iframe的字段很少,并且隐藏的按钮上具有单击事件,可以执行所有必要的操作(例如CRUD操作)。该帖子是通过ajax调用的,因此iframe上没有POST方法。父页面上有一个按钮,该按钮应在单击时使用,应触发iframe中的click事件。我确实研究了postMessage和跨域,并且能够找到我在解决方案上实现的东西。添加该内容后,当我单击父页面上的按钮时,什么也没有发生。我没有看到任何控制台错误,但没有任何反应。这就是我的代码。
<div id="main">
<iframe id="cardIframe" src="@($"https://localhost:44320/handlers?userId={Model.UserId}")"
width="100%" height="300px" style="border: none;" scrolling="no"></iframe>
<hr class="mb-4" />
@Html.UI().Button("Continue to Checkout", size: UIElementSize.Block, uiClass: UIElementClass.Primary, htmlAttributes: new { @class = "checkoutsubmit" })
<script type="text/javascript">
$('.checkoutsubmit').click(function (e) {
var iframe = document.getElementById('cardIframe').contentWindow;
window.iframe = iframe;
iframe.postMessage("createvault", "*");
});
</script>
在我的iframe应用程序(http://localhost:43402)中
<script type="text/javascript">
window.addEventListener("click", clickEvent, false);
function clickEvent(event) {
$('.processbraintree').trigger('click');
}
$('.processbraintree').click(function(e) {
..Do something
});
</script>
不确定在这里我在做什么错。我在控制台上看不到任何错误的事实无济于事。我必须让iframe负责表单的发布,因为它具有所有Braintree dropin ui内容。因此,父级必须能够在当前尚未发生的iframe中触发事件。