简体   繁体   English

表达:单击按钮后的POST请求

[英]express: POST request with button click

I want to insert data at a database table when a button in pressed. 我想在按下按钮时在数据库表中插入数据。 So I have the below code in my register.html file 所以我的register.html文件中包含以下代码

<form action="/send" method="POST">
          <input type="text" name="newID" placeholder="Enter your ID"/>
          <input type="submit" value="Send Data"/>
</form>

Following this post , I have below code in my app.js 在这篇文章之后 ,我的app.js包含以下代码

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

app.post('/send', function(req, res) {
  var newID = req.body.newID
  console.log(newID)
  res.sendFile(path.join(__dirname + '/background.html'));
}

Now when I write something in the text box and press the button, the page just moves to localhost:8080/send which shows the contents of background.html. 现在,当我在文本框中输入内容并按下按钮时,页面将移至localhost:8080/send ,其中显示了background.html的内容。 But I don't see the newID value in the javascript console . 但是我在javascript console newID不到newID值。

What am I missing? 我想念什么?

I WAS WRONG THIS IS NODE.JS NOTHING WILL BE PRINTED TO THE BROWSER CONSOLE 我错了,这是NODE.JS不会打印到浏览器控制台上

   pp.post('/send', function(req, res) {
      var newID = req.body.newID
      console.log(newID)
      res.sendFile(path.join(__dirname + '/background.html'));
    }

This is going to print "newID" to the console and then transfer you to a different page. 这将在控制台上打印“ newID”,然后将您转移到其他页面。 Not all web browsers store console data when your URL changes. 更改URL时,并非所有Web浏览器都存储控制台数据。 So it could be working but before you can view the console yourself it may be cleaning all the temporary data out to make room for whats coming on the next page. 因此它可以正常工作,但是在您自己查看控制台之前,它可能正在清除所有临时数据,以便为下一页中的内容腾出空间。 This could answer your question as to why you don't see anything. 这可以回答您为什么看不到任何东西的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM