blaster
blaster
CDCloudflare Developers
Created by blaster on 4/24/2024 in #workers-help
CORS Error - Trigger OPTIONS
So my server is hosted with workers and my client is hosted on another domain so keep running into the same errors regarding CORS. Here's a code snippet of the function that's causing the error. If anyone has any advice let me know.
async def on_fetch(request, env):
if request.method == "OPTIONS":
# Handle preflight requests
headers = Headers.new({
"Access-Control-Allow-Origin": "*", # Allow requests from any origin
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", # Specify allowed methods
"Access-Control-Allow-Headers": "Content-Type", # Specify allowed headers
})
return Response.new(None, status=204, headers=headers)
else:
# Handle other requests
payload = json.dumps({ "business": random.choice(business), "concept": random.choice(concepts) }, sort_keys=True)
headers = Headers.new({
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
})
return Response.new(payload, headers=headers)
async def on_fetch(request, env):
if request.method == "OPTIONS":
# Handle preflight requests
headers = Headers.new({
"Access-Control-Allow-Origin": "*", # Allow requests from any origin
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", # Specify allowed methods
"Access-Control-Allow-Headers": "Content-Type", # Specify allowed headers
})
return Response.new(None, status=204, headers=headers)
else:
# Handle other requests
payload = json.dumps({ "business": random.choice(business), "concept": random.choice(concepts) }, sort_keys=True)
headers = Headers.new({
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
})
return Response.new(payload, headers=headers)
I have the error log issues if they are necessary.
1 replies