簡體   English   中英

為什么Node.js沒有通過POST請求收到數據?

[英]Why is data not received by Node.js through POST request?

所以我有這個功能。 客戶端通過jquery post函數發送數據

$.post(currentURL + "/api/tables", newReservation,

  function(data) {

    if (data == true) {
      alert("Yay! You are officially booked!")
    }

    if (data == false) {
      alert("Sorry you are on the waitlist")
    }

    $('#reserve_name').val("");
    $('#reserve_phone').val("");
    $('#reserve_email').val("");
    $('#reserve_uniqueID').val("");

  });

然后Node.js在這里接收它

app.post("reserve/api/tables", function(req, res) {
  var newentry = req.body;

  console.log(newentry);

  entries.push(newentry);

  res.json(newentry);
});

但是, console.log給了我這個錯誤

jquery.js:9631 POST http:// localhost:8080 / api / tables 404(Not Found)

因為您正在對URL進行請求: http:// localhost:8080 / api / tables ,但您的服務器正在等待您:“reserve / api / tables”。

嘗試定位: http:// localhost:8080 / reserve / api / tables

您的路由指定localhost:8080 / reserve / api / tables,

刪除'保留'路由

你錯過了領先的斜線
app.post("reserve/api/tables", function(req, res) {
所以它應該是
app.post("/reserve/api/tables", function(req, res) {
要么
app.post("/api/tables", function(req, res) {
如后編輯

暫無
暫無

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

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