javascript - 为什么 google adsense 脚本不断返回 CORS 错误?
问题描述
我正在尝试将 adsense 放在我由 react 构建的网页上。自动广告工作正常,但手动广告在我的开发环境和生产环境中都会返回 CORS 错误。
Access to script at 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' from origin 'http://localhost:3050' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我将脚本标签放在 index.html 中。
<!DOCTYPE html>
<html lang="ko">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=xxxxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'xxxxxxxx');
</script>
<script crossorigin async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
jsx文件是这样的。
import React, { useEffect } from 'react';
import Grid from '@material-ui/core/Grid'
const MyPage = (props) => {
useEffect(() => {
(window.adsbygoogle = window.adsbygoogle || []).push({});
}, []);
return (
<Grid
container
justify="center"
>
<Grid item xl={3} lg={2}>
<div style={{
display: 'flex',
justifyContent: 'center',
minWidth: 160,
maxWidth: 320,
height: '100%',
alignItems: 'center',
flexDirection: 'column'
}}
>
<ins
className="adsbygoogle"
style={{ display: 'inline-block', width: '160px', height: '600px' }}
data-ad-client="xxxxxxxxxx"
data-ad-slot="xxxxxxxxxxx"
/>
</div>
</Grid>
<Grid item xl={3} lg={2}>
<div style={{
display: 'flex',
justifyContent: 'center',
minWidth: 160,
maxWidth: 320,
height: '100%',
alignItems: 'center',
flexDirection: 'column'
>
<ins
className="adsbygoogle"
style={{ display: 'inline-block' }}
data-ad-client="xxxxxxxxxxx"
data-ad-slot="xxxxxxxxxxxx"
data-ad-format="auto"
data-full-width-responsive="true"
/>
</div>
)}
</Grid>
</Grid>
);
};
export default MyPage;
我 在 React Firebase 网络应用程序中使用 Google Adsense 时尝试使用 CORS,并尝试使用 react-adsense 库。然而,他们都没有成功。我坚持了3天。有什么解决办法吗?
解决方案
我看到的与以前工作的唯一区别是我把...
...不在头部。我把它放在hte标签之间。
不确定这是否有助于我注意到的一件事,这与我过去尝试在我的 react 应用程序中实施 AdSense 时设置的不同。我确实希望这会有所帮助。
推荐阅读
- javascript - 如何处理深度嵌套的导航
- angular - 如何从api传递基于json的嵌套formarray动态形式的对象数组
- c++ - Drogon 为线程分配资源
- ios - swift scrollToRow 或 setContentOffset 不起作用
- python-3.x - 如何在 matplotlib 中更改 quiverplot [3d] 中的箭头属性?
- java - 我正在打开 netbeans,它正在加载,但屏幕上没有显示。当我再次打开它时,它只显示一个空白窗口,任何人都可以帮助我
- vba - 如果列没有特定值,则 VBA Excel
- python - 部署经过 Kaldi 训练的 ASR 模型以实时转录文本的方法
- google-anthos - 使用 Anthos Service Mesh 的 Google Traffic Director
- c++ - 如何使用winsock 2 异步接受套接字并从中读取数据?