http.send возвращает [объект-объект] на бэкэнде (узел + Express) - PullRequest
0 голосов
/ 26 мая 2020

мой http.send возвращает [объект-объект] в node.js бэкэнд.

    <script>
      const newExerciseForm = document.getElementById("newExercise");
      newExerciseForm.addEventListener("submit", function (e) {
        e.preventDefault();
        const http = new XMLHttpRequest();
        const userId = document.getElementById("uid").value;
        const description = document.getElementById("desc").value;
        const duration = document.getElementById("dur").value;
        const date = document.getElementById("dat").value;
        const params = userId + "," + description + "," + duration + "," + date;
        http.open(
          "POST",
          "http://localhost:5000/exercisetracker-f0756/europe-west1/exerciseTracker/api/exercise/add",
          true
        );

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        http.onreadystatechange = function () {
          //Call a function when the state changes.
          if (http.readyState == 4 && http.status == 200) {
            document.getElementById("exerciseTracker").innerHTML =
              http.responseText;
          }
        };
        http.send(params);
      });
    </script>

бэкэнд:

app.post('/api/exercise/add', (req, res) => {
  console.log(req.body)
  res.send('hitted');
})

Может кто-нибудь мне помочь, как решить эту проблему? Я выбрал стандартный метод xmlhttprequest из-за структуры приложения.

1 Ответ

0 голосов
/ 26 мая 2020

Terrymorse comment: использование "text / plain" мгновенно работает с настройкой в ​​вопросе.

 //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "text/plain");

Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...