-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to enable CORS #69
Comments
I have found a way to set proper headers, i.e. I solved the issue for myself. |
How did you manage to solve it? |
When returning response, you can pass headers like that: return new Response(body, { headers }) And before that you can set headers, something like that: const allowList = [
'https://jerrygreen.github.io',
// whatever origins you want to allow
]
const headers = new Headers({
'Content-Type': 'application/json; charset=utf-8',
})
const origin = request.headers.get('origin') || ''
if (allowList.includes(origin)) {
headers.set('Access-Control-Allow-Origin', origin) // <--- this is the key row
} Unfortunately it doesn't work for http by some reason, because either sift, or Deno, or Deno Deploy, or maybe even browsers, – some of them replace http with https for
But wildcard is not recommended for production, CORS have their reason behind that (basically, layer of security). Btw take notice that you can't pass the whole array to the header, and have to do your check, returning only the requested origin, – in case you allow such an origin. This is another little security measure. Returning |
@jerrygreen I don't even know how to thank you, thank you so much! |
@jerrygreen Unfortunately, even releasing access to all origins, I get a CORS error That's what I tried: const headers = new Headers({
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*'
})
"/simulation": async (req: Request) => {
if(req.method == "POST"){
const body = await req.text(); //return body
// Get and send simulation to Telegram bot
const dataForSimulation: SimulationFormat = JSON.parse(body);
const simulationResult = await simulator(dataForSimulation).then((result: SimulationFormat) => {
return result;
}).catch((err: any) => {
console.log(err);
});
return new Response(JSON.stringify(simulationResult), {
headers
});
}
return new Response("Hello", { headers });
}, This is the error I get: Access to XMLHttpRequest at 'https://karytonn-xxx.deno.dev/simulation' from origin 'https://www.xxx.com.br'
has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers
in preflight response. |
If you're using Github to deploy to Deno Deploy, it well might be that you pushed a commit with some syntax mistake or some non-existing variable or something. Therefore your deploy will be failed, and the older (working) code will be executed, while you're mistakenly convinced it is the new code is being executed. Make sure you have this tick in your Github repo: Also, make sure that you're accessing your main (production) deployment, not some old one: Normally urls like My overall recommendations would be:
|
@jerrygreen thanks for the light! This was missing: headers.set('Access-Control-Allow-Origin', '*');
+ headers.set('Access-Control-Allow-Headers', '*');
+ headers.set('Access-Control-Allow-Methods', 'GET,POST');
``` |
Currently I'm not able to request my little server function on Deno Deploy that I made with sift, because I'm getting an error:
How can I setup allowed domains for CORS with sift?
I haven't found any corresponding docs.
The text was updated successfully, but these errors were encountered: