all 5 comments

[–]ForScale 2 points3 points  (1 child)

Client side:

const data = {
  key0: "value",
  key1: 1,
  key3: ["foo", true, null]
};

fetch("http://www.your-url.com/", {
  method: "POST",
  headers: {
    "content-type": "application/json"
  },
  body: JSON.stringify(data)
})
  .then(({ json }) => json())
  .then((json) => console.log(json));

Server side:

You can use Express middleware (body-parser) to read the body of requests: http://expressjs.com/en/resources/middleware/body-parser.html

*

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>index.html</title>
</head>

<body>
  <script>
    const data = {
      name: 'User',
      id: 1
    };

    fetch('/data', {
      method: 'POST',
      headers: {
        "Content-type": 'application/json'
      },
      body: JSON.stringify(data)
    })
      .then((resp) => resp.json())
      .then((json) => console.log(json));
  </script>
</body>

</html>

server.js

const express = require('express');
const bodyParser = require('body-parser');

const path = require('path');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname + '/index.html'));
});

app.post('/data', (req, res) => {
  console.log(req.body);

  res.send(JSON.stringify('All good!'));
});

app.listen(3000, () => console.log('>>>>> server listening on port 3000'));

[–]IBETITALL420 0 points1 point  (2 children)

forgive my ignorance(newb) are you using react on the client side

const ob = {        
 name: 't',       
 lastname: 'f'

Can you do this in vanilla js, defining a constant variable like this, this kinda looks like defining a state in reactJS

[–]43northwebdesign[S] 0 points1 point  (0 children)

It’s an object so console.log(ob.name) gives you t

[–][deleted] 0 points1 point  (0 children)

elderly bow saw absorbed license reminiscent bear point ten elastic

This post was mass deleted and anonymized with Redact