简体   繁体   English

使用Ajax / jQuery发出POST请求以将行插入到节点js服务器上的mysql表中

[英]Using Ajax/jQuery to make POST request to insert row to mysql table on node js server

I have a form that takes in information such as name, age, etc, and the goal is to submit this information via the client side to insert this data into a mysql table row. 我有一个接受诸如姓名,年龄等信息的表格,目标是通过客户端提交此信息,以将该数据插入到mysql表行中。 However, I am not able to get the data to insert. 但是,我无法插入数据。 Here is my client side JS code (the form id is 'form1' and the form submit button id is 'formSub', and the mysql table name is 'formTable'): 这是我的客户端JS代码(表单ID为“ form1”,表单提交按钮ID为“ formSub”,而mysql表名称为“ formTable”):

function insertData() {
    document.getElementById('formSub').addEventListener('click', function(event) {
        var formQuery = $("#form1").serialize();

        $.post('/insert', formQuery);
    });
}

This is my server side code (node.js): 这是我的服务器端代码(node.js):

app.post('/insert', function(req, res, next) {
    mysql.pool.query("INSERT INTO formTable (`name`, `age`, `city`) VALUES (?, ?, ?)", 
        [req.body.name, req.body.age, req.body.city], function(err, result) {
            if (err) {
                next(err);
                return;
            }
    });
});

This is the error I get: 这是我得到的错误:

POST http://ipaddress:3000/insert 500 (Internal Server Error)    jquery.min.js:4

Navigated to http://ipaddress:3000/?name=Bob&age=20&city=NYC

The form seems to be taking the data by looking at what is navigated to, but I don't see this data in the mysql table when I go to look at it in mysql. 表单似乎是通过查看导航到的内容来获取数据的,但是当我在mysql中查看数据时,在mysql表中没有看到此数据。 I'm not sure what to do. 我不确定该怎么办。

"Navigated to http://ipaddress:3000/?name=Bob&age=20&city=NYC " Doesn't that mean it's somehow sending the data via GET and not POST if you're URL encoding? “已导航到http:// ipaddress:3000 /?name = Bob&age = 20&city = NYC ”,这是否意味着通过URL编码以某种方式通过GET而不是POST发送数据?

Is this using Express 3 or 4? 这是使用Express 3还是4?

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

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