所以我开始用 cloudflare pages 和 workers 弄脏我的手
我的想法是
- 获得一个域
mydomain.com
(完成) - 制作静态站点@github
myaccount.github.io
(完成) - 制作一个 cloudflare 页面页面,将 CNAME 添加到 DNS ZONE(完成),这样
myaccount.github.io
可以@my-official-site.com
(完成) - 做一个 cloudflare 工作者
https://myworker.myclaccount.workers.dev
(完成)
所以当我要求my-official-site.com
我得到的内容myaccount.github.io
现在我想从我myaccount.github.io
的工人那里跑来获取一些 html 或 json 或....
- 我试图从我的
myaccount.github.io
(index.html)中获取
<script>
fetch('https://myworker.myclaccount.workers.dev')
.then(response => response.json())
.then(data => console.log(data));
</script>
- 我收到这样的请求给我的工人
const html =
"<p>This markup was generated by a Cloudflare Worker.</p>"
async function handleRequest(request) {
return new Response(html, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest(event.request))
})
问题是
Access to fetch at 'https://myworker.myclaccount.workers.dev/'
from origin 'my-official-site.com' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我创建一个
_headers.txt(https://developers.cloudflare.com/pages/platform/headers)在我的github“站点”的根目录中
/*
Access-Control-Allow-Origin: *
但同样...
我是无服务器和 jamstack 的新手(我现在是 js、css、html),但对这个概念很陌生
知识(android、c#、php、wp、unity、一些node.js)
谢谢您的帮助。