【问题标题】:Cross domain messaging using postMessage使用 postMessage 跨域消息传递
【发布时间】:2019-02-04 21:01:24
【问题描述】:

我有一个要求,我需要在 iframe 中打开一个 html 页面,而该页面又放置在另一个 html 页面中(此页面的 uri 以 file:// 开头)。 iframe 和外部 html 页面之间需要有跨域消息传递。当 iframe 内的页面从本地机器加载时,它工作得非常好(外部页面和 iframe 中的页面都有 file:// uri)。

现在,如果我尝试创建一个应用程序,请将其托管在我的本地计算机中并在 iframe 中打开它,虽然页面成功加载,但外框与 iframe 中的页面之间的消息传递失败。

我在 Chrome 中收到以下错误:-

Uncaught SecurityError: Blocked a frame with origin "http://localhost/newapp" 
from accessing a frame with origin "null".  The frame requesting access has a protocol of 
"http", the frame being accessed has a protocol of "file". Protocols must match.

我已经允许托管应用程序的服务器上的跨域请求。我还可以尝试哪些其他步骤来使 postMessage 通信成为可能?

【问题讨论】:

    标签: javascript html http iframe postmessage


    【解决方案1】:

    错误信息很明确:您使用的不是相同的协议。一种是使用 HTTP,另一种是使用文件。

    作为 Chrome 的安全选择,您需要在 HTTP 或文件协议下提供服务。

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 2016-12-04
      • 2013-05-27
      • 1970-01-01
      • 2012-01-01
      • 2015-11-20
      • 2010-11-27
      • 1970-01-01
      相关资源
      最近更新 更多