簡體   English   中英

http.send 在后端返回[object object](Node + Express)

[英]http.send returns [object object] at the backend (Node + Express)

我的 http.send 在 node.js 后端返回 [object object]。

    <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 方法。

特里莫爾斯評論:使用“text/plain”可以立即與問題中的設置一起使用。

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

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM