使用按钮的NodeJS发布请求 [英] NodeJS Post request using a Button

查看:104
本文介绍了使用按钮的NodeJS发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行.我所做的所有研究都表明,使用表格和文本输入是可能的.但是无论如何,使用NodeJs和Express,我希望能够单击网页上的一个按钮,然后单击该按钮,它将向我的Node.JS服务器发送发布请求.

I don't know if this is possible or not. All the research I've done has shown that it is possible with a form and text input. But anyways, Using NodeJs & Express I want to be able to click a button on my webpage, and once it's clicked, it sends a post request to my Node.JS server.

简单的说法: 单击按钮后,将信息发送到服务器.

Simpler way of saying it: When button is clicked, send info to the server.

我正在努力实现的目标: 单击按钮后,它会发送某种ID/代码/任何东西来打开我的数据库中的服务. (我尚未了解db的工作方式,因此我只是想专注于前端.)

Goal I'm trying to achieve: When button is clicked, it sends some sort of ID/code/anything to turn on a service from my database. (I have yet to learn how db's work so I am just trying to focus on front end.)

到目前为止我拥有的代码:

Code I have so far:

app.post("/send", function(req, res){
  var newID = req.body.ID;
  res.redirect("/action")
});

<form action="/send" method="POST">
    <input type="button" name="newID" placeholder="Button">
    <button>send</button>
</form>

推荐答案

您不需要使用jQuery或AJAX.

You do not need to use jQuery or AJAX.

只需在form标记内添加类型为submitinput,以便提交由您的form标记定义的POST请求.

Simply add an input of type submit inside the form tag so that the POST request defined by your form tag is submitted.

您的newID输入应为text类型,这样可以在输入字段中输入一个值.

Your newID input should be of type text, this allows entering a value in the input field.

可以使用req.body.newID在服务器端检索newID值(请确保使用body-parser中间件).

The newID value can be retrieved server side with req.body.newID (be sure to use the body-parser middleware).

<form action="/send" method="POST">
    <input type="text" name="newID" placeholder="Enter your ID"/>
    <input type="submit" value="Click here to submit the form"/>
</form>

这篇关于使用按钮的NodeJS发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆