简体   繁体   English

在不同的域上重定向iframe的父级 - 替代逻辑

[英]Redirect Parent of iframe on different domain - alternative logic

I know similar questions have been asked before, but I was wondering if anyone could help find a solution to a particular problem: 我之前已经问过类似的问题,但我想知道是否有人可以帮助找到特定问题的解决方案:

I have a form that I'd like to embed in a few different web sites, on different domains. 我有一个表格,我想嵌入不同领域的几个不同的网站。 This form needs to redirect to PayPal. 此表单需要重定向到PayPal。 The plan was to embed this form in an iframe, making it easy to update etc, plus minimal code to embed. 计划是将这个表单嵌入到iframe中,使其易于更新等,加上最少的嵌入代码。 However, due to same origin policy I cannot use parent.location.href = "http://PaypalPaymentAddress.com"; 但是,由于相同的原始政策,我不能使用parent.location.href = "http://PaypalPaymentAddress.com"; to redirect the form. 重定向表单。 I don't want to process the payment in the frame (Paypal anyway forbids this)... 我不想在框架中处理付款(Paypal无论如何都禁止这个)...

The only way around this I can think of is having a script run and render the form into the parent page, calling a web service or so to get the HTML, but that would obviously be a lot of work! 我能想到的唯一方法是运行脚本并将表单呈现到父页面中,调用Web服务或者获取HTML,但这显然会有很多工作!

Any ideas how I might set this up to get it working? 任何想法我如何设置它以使其工作?

您可以使用目标属性"_parent"作为表单,当它将被提交时,目标URL将被加载到父窗口中。

My final method was based on Dr. Molle's answer, but slightly different. 我的最终方法是基于Molle博士的答案,但略有不同。 I have explained it here in case it is useful to anyone else: 我在这里解释过,以防它对其他人有用:

I set up the form to submit normally, allowing all my server validation etc to only fire once. 我将表单设置为正常提交,允许我的所有服务器验证等只触发一次。 If the form submitted successfully (passes the server validation), a small html client page, containing an empty form was posted back. 如果表单成功提交(通过服务器验证),则会回发一个包含空表单的小型html客户端页面。

This empty form has a post action of the parent page URL, and a target = "Parent" and on the client "document ready", a jQuery method submits the form, causing the parent page to reload. 此空表单具有父页面URL的post操作,target =“Parent”,并且在客户端“document ready”上,jQuery方法提交表单,导致父页面重新加载。

A link with a "_top" attribute is also provided in case scripts are disabled. 如果禁用脚本,还会提供带有“_top”属性的链接。

<script type="text/javascript">
    jQuery(document).ready(function () {
        $("#redirectForm").submit();
    });
</script>
<a href="{parentpageurl}" id="redirectLink" target="_top">click here if not redirected automatically</a><br />

<form id="redirectForm" method="POST" action="{parentpageurl}" target="_parent">

</form>

One way of getting round the "Same Origin Policy", as this method works cross domain. 绕过“同源策略”的一种方法,因为此方法适用于跨域。 Hope that helps somebody! 希望能帮到别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM