i\'m struggling with the fetch API in javascript. When i try to POST something to my server with fetch, the request body an empty array. But when i use Postman it works... H
To complement the excellent answers that mentioned removing the mode: 'no-cors' option, and if you don't feel like adding a CORS middleware to Express, you may just want to handle "pre-flight" requests under an OPTIONS block:
app.options('*', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); // Add other headers here
res.setHeader('Access-Control-Allow-Methods', 'POST'); // Add other methods here
res.send();
});
Another nice answer here : https://www.vinaygopinath.me/blog/tech/enable-cors-with-pre-flight/
Hope this helps!