0

good day!

Please tell there are misstake. When project run - CountValue is {}

i have fetch to send data from react:

let data = {pp: number};
    data = JSON.stringify(data);


    fetch('http://localhost:3001/number', {
        method: 'post',
        body:  data
    }).then(function(responce) {
        console.log('data submit success');
    }).catch(function(error) {
        console.log('got err', error);
    })

this is the server code

var bodyParser= require('body-parser');

app.use(bodyParser.json());
router.post('/', function (req, res) {
  var countValue = req.body;
  console.log('CountValue is', countValue);
});

How should I send data?

alivadjid
  • 1
  • 4

1 Answers1

1

You forgot the headers in your fetch:

fetch('http://localhost:3001/number', {
    method: 'post',
    body:  data
    headers: { "Content-Type": "application/json"}
HermitCrab
  • 3,194
  • 1
  • 11
  • 10
  • Thank, it's work! But when try in Google CORS, block access. "is not allowed by Access-Control-Allow-Headers in preflight response". – alivadjid Apr 22 '20 at 12:30
  • I have next cors in server: – alivadjid Apr 22 '20 at 12:30
  • res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers","Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Headers"); res.header("Access-Control-Allow-Headers", "Origin, http://localhost:3000"); res.header("Access-Control-Allow-Headers","append,delete,entries,foreach,get,has,keys,set,values,Authorization"); res.header("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE, PUT, HEAD"); res.header("Access-Control-Allow-Credentials", "true"); – alivadjid Apr 22 '20 at 12:31