-
Notifications
You must be signed in to change notification settings - Fork 539
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 is not working with non GET methods #1806
Comments
I'm facing this as well, spend a day trying to figure out why I just can't get CORS to work. I have no idea why there are so many ways that seem to do the same thing: handleCors()
appendCorsHeaders()
appendCorsPreflightHeaders() and of course, the I also noticed inconsistent behavor if you use any of the above methods in the handler itself, or a middleware. If anyone has a workaround to at the very least be able to just disable CORS globally, until this is fixed. |
I created a build of my nuxt / nitro project on Render Another thing I tried I tried and noticed was, I created the following two: A minimalistic Vue SSR app using Vike {
"name": "vike-app",
"version": "1.0.0",
"description": "",
"private": true,
"type": "module",
"scripts": {
"dev": "npm run server:dev",
"prod": "npm run build && npm run server:prod",
"build": "vite build",
"server:dev": "node ./server",
"server:prod": "cross-env NODE_ENV=production node ./server"
}, A basic, bare bones Express server {
"name": "test",
"version": "1.0.0",
"description": "",
"private": true,
"type": "module",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
...
} It's important to note, that Vike uses Vite whilst my bare bones Express server doesn't const ALLOWED_ORIGINS = ['http://localhost:3000', 'http://localhost:5000']
app.use(cors({
origin (origin, callback) {
if(!origin) return callback(null, true); // allow requests with no origin - (like mobile apps or curl requests)
if (!ALLOWED_ORIGINS.includes(origin)) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.'
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true
})); With the Express.js server, as per my config, all HTTP Methods including I'm just wondering if the common factor of CORS issues with Nuxt / Nitro, Vike is linked to Vite or its configuration, since Express worked fine? Thoughts on this? |
Same issue, do you have a solution ? |
Same issue here with POST |
I suspect that this is an issue with preflight requests not being handled correctly. @Ibz786 can you provide a Github Repo with both the server implementation and the client code that is calling the API. The sandbox provided only includes the server part if I'm not mistaken. |
Anyone found a solution? Also have this issue. |
Closing in favor of unjs/h3#744, a PR is already submitted for it 👍🏻 |
Hi guys,
I've been stuck on an issue for the longest time and have tried everything I can think of, but can't seem to figure out the problem
I have a Vue 3 app (localhost:3000) and a Nuxt 3 app, using Nitro (localhost:3500)
In Nuxt/Nitro, I have created some
server/api
routes, which I'm trying invoke from my Vue 3 application, via Axios (using withCredentials)I've set up some basic CORS configuration, and when it comes to simple
GET
methods, all works fine. However when it comes toPOST
, I get aPOST + Preflight
issue as follows:Access to XMLHttpRequest at 'http://localhost:3500/api/v1/auth/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
I've tried many ways to setup CORs from
middleware
, torouteRules
and now I've just tried usingnuxt-security
module, but unfortunately, no differenceMy code is as follows:
Vue 3:
Nuxt 3 / Nitro:
GET
method example works finePOST
example doesn't workI've tried to handle CORS in the following ways:
middleware
Method 1middleware
Method 2, from: https://www.reddit.com/r/Nuxt/comments/109au1x/how_so_enable_cors_in_nitro/nuxt.config.ts
Method:routeRules and nitro: routeRules
nuxt.config.ts
Method:nuxt-security
https://nuxt-security.vercel.app/getting-started/setupFor reproduction steps I've setup the following:
https://stackblitz.com/edit/github-wovvta
If anyone has any ideas with regards to this it would really be much appreciated, thanks!
The text was updated successfully, but these errors were encountered: