#help endpoints cors
12 messages · Page 1 of 1 (latest)
Have you had a look at: https://docs.astro.build/en/reference/configuration-reference/#serverheaders
I made a function called headers:
export const getHeaders = (origin) => {
if (
origin === 'tauri://localhost' ||
origin === 'https://tauri.localhost' ||
origin === 'http://localhost.atavismxi.com:4321' ||
origin === 'http://localhost:1420' ||
origin === 'https://atavismxi.com'
) {
return {
'Access-Control-Allow-Origin': origin,
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Credentials': 'true',
}
}
}
Then I import my getHeaders function in each endpoint: import { getHeaders } from '../../../utils/headers'
Pass origin to the headers function:
export async function GET({ cookies, request }) {
const origin = request.headers.get('origin')
const headers = getHeaders(origin)
}
Put headers in the response:
return new Response(JSON.stringify(results), {
status: 200,
headers: headers,
})
Maybe this is crazy?
Hello, thank you for your answers. I tried both options and configurations and it still doesn't work. I share the configurations I made.
I couldn't get CORs errors to go away unless I set the headers in each endpoint. If that makes sense. Like in each GET, POST,PUT etc. Not sure what the right terminology is
I tried setting them for the server too but didn't affect CORS for me
oh I just saw the screenshot for the endpoint
if the post is still giving you CORs errors maybe trying settings it's access-control-allow-origin to * for testing purposes
do you see any console logs like this pic where it shows you where the request origin is?
In both development and production I get the same error. I have tried placing the specific path and also the *