是否可以从iframe中重定向到新的来源?

时间:2018-05-22 21:43:24

标签: javascript iframe

我有类似的东西:

<iframe src="https://foo.com"></iframe>

我希望https://foo.com重定向到https://bar.com

我已经尝试过服务器端重定向(https://foo.com返回302)和客户端重定向(foo.com包含一个window.location.href = https://bar.com的脚本),但却阻止了跨域浏览器发送帖子消息时出错:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://foo.com') does not match the recipient window's origin ('https://bar.com').

iframe中的重定向是否可能?

1 个答案:

答案 0 :(得分:0)

是的,您可以重定向。

听起来你也成功了。

...但是如果您重定向到其他来源,那么您将重定向到其他来源。任何跨源请求都需要授予新原点的权限。

http://foo.com/重定向到http://bar.com并未授予bar.com授予foo.com的所有权限。

如果您要将postMessage发送到bar.com,那么您需要将该消息标记为预期的来源。