Skip to content
Advertisement

Access to XMLHttpRequest at ‘http://localhost:8080/’ from origin ‘http://localhost:3000’ has been blocked by CORS policy

Full error: “Access to XMLHttpRequest at ‘http://localhost:8080/api/tutorials’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

On the frontend I am using NuxtJS. I am sending a simple post request at the node server hosted on localhost:3000, but it’s giving me this error even though I am using CORS in my app. The request is for inserting data in the mongodb database.

On the node server i.e. localhost:8080, I’ve tried:

JavaScript

Tried this too but no success:

JavaScript

Please tell if there is any solution for this error. Thank you.

EDITED:

Here, I am using axios to first create an instance (on the frontend). https://ibb.co/C2ChH5d

These are all the requests that I want to use. https://ibb.co/5TbSHjF

And this is the request that is being made currently(create). https://ibb.co/pPp8rdh

The nodeJS code: https://ibb.co/GFWMQW1

NodeJS code:

JavaScript

Advertisement

Answer

Well, the problem was that I was using the API routes before initializing cors(). When I shifted the routes down the app.use(cors(corsOptions)), the problem was solved.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement