首页 > 解决方案 > postMessage 总是返回错误的数据?

问题描述

我的目标是单击当前选项卡中的一个按钮,它将打开一个新选项卡,并在新选项卡的 Chrome 开发工具中使用 postMessage API:

window.opener.postMessage({deliveryId: 18042548, materialId: 1051041576634029702},'*')

当前选项卡已经添加了onmessage事件,它将检查来源,console.log数据:

window.addEventListener("message", this.syncDirectMaterialTag)
syncDirectMaterialTag = async e => {
    if ('http://beta-ace.jd.com' === e.origin) {
      console.log(e.data)
      //const res = await queryDirMaterialTag(mapper)
    }
  }

但无论我发布什么,它总是返回相同的错误信息:

deliveryId: 18042548
materialId: 1051041576634029700

怎么会这样 ???

标签: javascriptreactjspostmessage

解决方案


我已经修好了。

原因是 JavaScript 失去了准确性。

所以materialId的类型应该是String:

window.opener.postMessage({deliveryId: 18042548, materialId: '1051041576634029702'},'*')

推荐阅读