【发布时间】:2014-01-19 11:48:55
【问题描述】:
我有两个站点:本地:20279/ 和本地:54220。 54220 从 20279 开始通过 IFrame 打开。用户单击按钮后,IFrame 将关闭,我在 52220 中获取数据。我阅读了如何启用 CORS,这就是我在 web.config 中所做的:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
文件js中的这段代码用来读取IFrame里面的数据
function dialog_close() {
dialog(false, '');
getValueFromIFrame("if-dialog", "status");
}
function getValueFromIFrame(idIFrame, idField) {
var iframe = document.getElementById(idIFrame);
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var value = innerDoc.getElementById(idField).value;
alert(value);
//return value;
}
这是我从浏览器得到的:
Blocked a frame with origin 20279 from accessing a frame with origin "http://localhost:54220". Protocols, domains, and ports must match. <br/>
Uncaught SecurityError: Blocked a frame with origin "http://localhost:20279" from accessing a frame with origin 54220. Protocols, domains, and ports must match.
我的问题是:如何让它发挥作用?
谢谢
【问题讨论】:
-
您的问题与CORS无关。您需要阅读网络消息 API,因为您需要利用它与不同来源的 iframe 进行通信。
-
@RayNicholus:它有效。谢谢你的帮助。您可以发布一个我可以标记的答案吗?
-
如果你有一个可行的解决方案,你应该发布代码作为答案。
标签: javascript asp.net http-headers cors