Django 对同源策略添加豁免(仅针对一个站点)

Django add exemption to Same Origin Policy (only for one site)

当我尝试通过 html.

在我的网站上加载 google 广告时,我收到 google.g.doubleclick.net 抛出的错误
Blocked a frame with origin "https://googleads.g.doubleclick.net" from accessing a frame with origin "https://example.com". Protocols, domains, and ports must match.

奇怪的是,我的网站有一个部分,我通过 javascript 添加了一些广告,并且该部分没有抛出任何错误。

我读到有关向站点根目录添加 crossdomain.xml 的信息,我尝试了(并且还使用 NGINX 为其提供服务,但这也不起作用...

有什么方法可以为 django 的 CSRF 规则添加例外,或者有什么其他方法可以解决这个问题吗?这让我发疯。此错误仅在 safari 中抛出(仅尝试过 safari 和 chrome),但它为加载页面的数据传输增加了很多,我不希望速度变慢。

这与 CSRF 无关,而是与 same origin policy security restriction which you can fix by implementing CORS 和发送适当的 headers.

有关

您可以使用 django-cors-headers 来帮助解决这个问题。