简体   繁体   English

Node.JS + mySQL -> 如何调用 ajax 并访问 server.js 上的数据?

[英]Node.JS + mySQL -> How to do the ajax call and access the data on server.js?

So this is my server.js file, where the db is being connected to :所以这是我的 server.js 文件,数据库正在连接到:

var app = require('express')();
var mysql = require('mysql');
var db = mysql.createConnection({

    host: 'localhost',
    user: 'root',
    password: '',
    database: 'user_data',
    port: 3306
});
db.connect();

app.get('/', function (req, res) {
    res.sendFile(__dirname + "/start.html");
});

app.listen(3000);

So I want to make something like a registration form, where you input your username and password.所以我想做一个类似于注册表的东西,在那里你输入你的用户名和密码。 Then, after you click the submit button, the username and password are supposed to go straight to the mySQL database.然后,在您单击提交按钮后,用户名和密码应该直接进入 mySQL 数据库。 So apparently, I need an ajax call which sends the data to that server, but I don't know how to access it on the server.js.显然,我需要一个将数据发送到该服务器的 ajax 调用,但我不知道如何在 server.js 上访问它。

$("button#submit").click(function () {
      $.ajax({
           'url': 'http://localhost:3000',
           'type': 'post',
           'data': {
                'username': $("#usr").val(),
                'password': $("#pwd").val()
           },
           'success': function (data) {
                alert('Data: ' + data);
           }
        });
  })

After I get that data on the server.js, I will obviously want to make a query (to insert the values) but how do I get the data?在 server.js 上获取该数据后,我显然想要进行查询(以插入值),但如何获取数据?

This is how I did it for one of my projects:这就是我为我的一个项目所做的:

HTML: HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Contact Form</title>

</head>
<body>
<div id="contact">
    <h1>Send an email</h1>
    <form action="/myaction" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" placeholder="Enter your full name" />

            <label for="location">Location:</label>
            <input type="text" id="location" name="location" placeholder="Enter your location" />

            <input type="submit" value="Send message" />

        </fieldset>
    </form>
</div>
</body>
</html>

Now when user clicks submit /myaction url will hit.现在,当用户点击提交 /myaction url 将命中。

db.connect(function(err,connection){
    app.post('/myaction',function(req,res){
    console.log(req.body);

     var employee={name:req.body.name,location:req.body.location}

    db.query('INSERT into employees SET ?',employee,function(err,res){
    if(err){
        throw err;
    }
        else{
        console.log(res);

    }


        })
   res.send(JSON.stringify(req.body));
})
})

Make sure to include these in your server.js确保在你的 server.js 中包含这些

var express=require('express');
var bodyParser=require('body-parser');
app=express();

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

So you can see i have defined app.post and I am hitting my url.所以你可以看到我已经定义了 app.post 并且我正在点击我的网址。 Then we simply write a sql query to store data.然后我们简单地写一个sql查询来存储数据。

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

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