Answer the question
In order to leave comments, you need to log in
Why is an empty POST coming to node server?
I'm trying to send data via fetch to my server on node:
fetch('http://localhost:3001/translate',
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
mode: "no-cors",
body: JSON.stringify({"aaa": "form"})
}
const express = require('express')
const mongoose = require('mongoose');
const bodyParser = require('body-parser')
const translated = require('./models/Translated')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
let db;
mongoose.connect('mongodb://localhost:27017/local', (error, database) => {
if(error) {
return console.log('error');
}
db = database;
})
app.get('/', (req, res) => res.send('Hello World!'))
app.post('/translate', (req, res) => {
console.log(req.body, 'ds');
})
Answer the question
In order to leave comments, you need to log in
Try adding
"Content-Length" to headers: Buffer.byteLength(JSON.stringify({"aaa": "form"}),"utf8")
You need to send not to localhost:3001
but to an external address of the form192.168.xx.xx:3001
Try like this:
fetch('/translate',
{
method: 'POST',
headers: new Headers({
Accept: 'application/json',
'Content-Type': 'application/json'
}),
mode: 'same-origin',
body: JSON.stringify({ aaa: 'form' })
}
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question