Skip to content
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

CORS error when using api client side #32

Open
samuelkarani opened this issue Dec 8, 2024 · 2 comments
Open

CORS error when using api client side #32

samuelkarani opened this issue Dec 8, 2024 · 2 comments

Comments

@samuelkarani
Copy link

samuelkarani commented Dec 8, 2024

Localhost works but any other client domain/host throws a CORS error

@Feel-ix-343
Copy link
Contributor

Hey! Could you give some code to reproduce?

@samuelkarani
Copy link
Author

samuelkarani commented Dec 14, 2024

Here's a code sample: https://stackblitz.com/edit/vitejs-vite-2ydmyfsa?file=src%2FApp.tsx

Make sure to add a valid api key.

The same code works when in localhost or a server.

Obviously the fix here would be returning the right client headers to satisfy CORS such as

res.setHeader('Access-Control-Allow-Credentials', true)
  res.setHeader('Access-Control-Allow-Origin', '*')
  // another common pattern
  // res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
  res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT')
  res.setHeader(
    'Access-Control-Allow-Headers',
    'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
  )
  if (req.method === 'OPTIONS') {
    res.status(200).end()
    return
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants