I have an edge function in Javascript on Cloudflare Workers. The only thing it does is to check a specific header and return a JSON containing a value accordingly
See code below
async function handleRequest(request) {
  const url = new URL(request.url);
  const psk_db = await KV_STORAGE.get(request.headers.get(PRESHARED_AUTH_HEADER_KEY));
  if (psk_db === null) {
    return new Response("Access denied", { status: 404 });
  }
  else{
    //calculate number
    //return JSON
    const data = {
      pswd: psk_db,
    };
    json = JSON.stringify(data, null, 2);
  }
  return new Response(json, {
      headers: {
        'content-type': 'application/json;charset=UTF-8',
        'Access-Control-Allow-Origin': url.origin,
      },
    })
}
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});
Now, the function works fine on the cloudflare test envirovment but when I try to request from an html page with a button that run this javascript function
function RequestCode() {
  const Http = new XMLHttpRequest();
  const url = "https://code-return.dev-malv.workers.dev";
  Http.open("GET", url);
  Http.setRequestHeader("Access-Control-Allow-Origin", "*");
  Http.setRequestHeader("X-Custom-PSK", "m_custom_key");
  Http.send();
  Http.onreadystatechange = (e) => {
    console.log(Http.responseText);
  };
}
I got the error
Access to XMLHttpRequest at 'my_workers_url' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have added on both side the Access-Control-Allow-Origin at * but it doesn't work
What can I do to avoid the error?
 
    