So i started to get my hands dirty with cloudflare pages and workers
My thought is
- get a domain
mydomain.com(done) - make a static site @ github
myaccount.github.io(done) - make a cloudflare pages page add CNAME to DNS ZONE (done) so
myaccount.github.iocan be @my-official-site.com(done) - make a cloudflare worker
https://myworker.myclaccount.workers.dev(done)
So when i request my-official-site.com i get the content of myaccount.github.io
Now i want to run from my myaccount.github.io a worker to get some html or json or....
- I trying to fetch from my
myaccount.github.io(index.html)
<script>
fetch('https://myworker.myclaccount.workers.dev')
.then(response => response.json())
.then(data => console.log(data));
</script>
- I get the request to my worker like this
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))
})
The problem is
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.
I create a
_headers.txt (https://developers.cloudflare.com/pages/platform/headers) at the root of my github "site" with this
/*
Access-Control-Allow-Origin: *
but the same...
I'm new to serverless and jamstack (i now js, css, html) but new to this concept
knowledge (android, c#, php, wp, unity, some node.js)
Thank you for your help.
At the top of the Cloudflare documentation, it states
I believe this is the issue you are encountering. You need to apply the headers directly in your worker function, as specified here -
https://developers.cloudflare.com/pages/how-to/add-custom-http-headers
On a side note, I'd recommend updating your question title to reflect what is being asked.