server code
JavaScript
x
11
11
1
const express = require('express');
2
const app = express();
3
4
app.use(express.json());
5
app.use(express.urlencoded({ extended:true }));
6
7
app.post('/',(req,res)=>{
8
console.log(req.body)
9
})
10
11
client code
JavaScript
1
9
1
const data = {idk:'stuff',idk2:'otherstuff'};
2
3
fetch('http://localhost:4000', {
4
method:'POST',
5
headers:{'Content-Type': 'application/json'},
6
mode:'no-cors',
7
body: JSON.stringify(data)
8
})
9
terminal shows: {} tried pretty much everything so help would be very much appreciated thanks in advance:)
Advertisement
Answer
You said mode: 'no-cors'
which tells fetch
to silently ignore anything which requires CORS permission.
Setting the Content-Type
to application/json
requires CORS permission so your request is sent with Content-Type: text/plain
instead.
This doesn’t trigger the JSON body parser.
- Don’t use
mode: 'no-cors'
. - Do configure your server to give permission using CORS