gpt4 book ai didi

javascript - node、sql 和 jquery -> 注册表单 - 访问服务器上的数据

转载 作者:行者123 更新时间:2023-11-29 19:12:10 24 4
gpt4 key购买 nike

所以我有一张注册表。当我输入详细信息并单击提交按钮时,该数据应该直接进入 mySQL 数据库。这是我的 index.html 部分,当我单击提交按钮时,我会在其中执行操作。

            $("#reg button#submit").click(function () {
$.ajax({
type: "POST",
url: "http://127.0.0.1/reg",
data: {
username: $("#usr").val(),
password: $("#pwd").val()
},
success: function (res) {
//
}

});
});

现在我使用 ajax 调用保存该数据,但如何在 server.js 上访问它?

我想它是通过 app.get() 但下一步该怎么做?

app.get('/reg', function (req, res) {
/// get the username and password and do a query and send them to database. How?
});

最佳答案

首先,您通过“POST”从前端发送数据,但您在后端等待“GET”请求。更改:

app.post('/reg', function (req, res) {
// Your data is in req.body.username and req.body.password
});

然后您可以将其插入数据库。

关于javascript - node、sql 和 jquery -> 注册表单 - 访问服务器上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42994289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com