Angular集成Google AdSense遇postMessage跨域错误的解决问询
Angular集成Google AdSense广告时postMessage跨域错误排查
加载含广告的组件时广告无法显示,报错信息如下:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://tpc.googlesyndication.com') does not match the recipient window's origin ('null').
已尝试的解决方案
- 使用ng2-adsense库,按文档配置后尝试延迟2秒渲染广告:
同时在index.html中测试了两种脚本:仅含async src的脚本,以及Google推荐的带client和crossorigin参数的脚本。ngAfterViewInit(): void { setTimeout(() => { this._adTime = true; }, 2000); } - 不使用库,采用Stack Overflow上的Angular 2集成AdSense方案。
- 修改协议,将index.html中的脚本URL从https改为http,并在proxy.config.json中设置api.secure为true:
{ "/api": { "target": "https://localhost:4000", "secure": true } } - 创建新的广告单元(原单元已创建多日,不存在新单元未生效问题)。
疑问
该错误是否仅在本地环境出现,必须部署到线上才能测试?
内容的提问来源于stack exchange,提问作者Duoxr
相关产品推荐
相关产品推荐

