【问题标题】:Unable to use postMessage within cordova无法在科尔多瓦内使用 postMessage
【发布时间】:2013-12-12 15:44:08
【问题描述】:

在我们的 cordova/phonegap-app 中,我们使用 iFrame 来加载外部网站。为了与 iFrame 内容通信,我为使用 iFrame.contentWindow.postMessage() 方法的两个站点(应用程序和网站)创建了一个脚本。原因是在智能手机的系统浏览器中打开 iframe 内的外部链接。该解决方案在cordova 2.3.0 上运行良好。

今天我已将应用程序的cordova 版本更新为3.1.0,并从android-manifest 中删除了一些权限。但是与 iFrame 内容的通信不起作用。显示以下错误错误消息:

"Unable to post message to https://[domain]. Recipient has origin file://"

要向 IFrame 发布消息,我使用以下代码:

$('#webViewIFrame').get(0).contentWindow.postMessage('message', host);

有人知道为什么我的通信解决方案不适用于 cordova 3.1.0 吗?

【问题讨论】:

标签: javascript android iframe cordova postmessage


【解决方案1】:

通常这应该是因为跨域问题。试试这个:

$('#webViewIFrame').get(0).contentWindow.postMessage('message', '*');

【讨论】:

    【解决方案2】:

    你需要使用:

    $('#webViewIFrame').get(0).contentWindow.postMessage('message',"*");
    

    因为 phonegap/cordova 页面在“file://”和https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage提供服务

    "...在文件的页面上发布消息:URL 当前要求 targetOrigin 参数为“*”。file:// 不能用作安全限制;将来可能会修改此限制。 "

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2015-02-12
      • 2015-08-13
      相关资源
      最近更新 更多