我如何使用 fetch 将更新的变量从客户端发送到服务器?

How do i send an updated variable from client to server with fetch?

我正在尝试将更改后的变量从客户端发送到服务器,但我无法弄明白。我想要实现的是我可以在 main.ejs.

中使用它
<script>
document.getElementById("char2btn").addEventListener("click", change)
function change() {
  charOneInView = "Yes"
  const data = {charOneInView}
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  };
  
  fetch('/welcome2', options)
}
  </script>

Index.js:

var  charoptioneOne = "No"

router.post('/welcome2',  ensureAuthenticated, function(req, res){
   console.log(req.body)
   res.redirect('/main'); 
  })
  
router.get('/main', ensureAuthenticated, (req, res) =>
  res.render('main',  {
    user: req.user,
    charOneInView: charoptioneOne,
    charTwoInView: charoptioneTwo
  })
)

对我来说它看起来仍然是一个对象而不是一个改变的变量,我该如何解决这个问题?

在后端使用 body-parser 模块

const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

像这样设置请求头:

  headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }