You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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秒渲染广告:
    ngAfterViewInit(): void {
      setTimeout(() => {
         this._adTime = true;
      }, 2000);
    }
    
    同时在index.html中测试了两种脚本:仅含async src的脚本,以及Google推荐的带client和crossorigin参数的脚本。
  • 不使用库,采用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:52:46