欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

浏览器传参

程序员文章站 2024-01-27 08:10:34
...

GET传参

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>登录动画</title>
    <link rel="stylesheet" href="css/index.css">
</head>

<body>
    <form class="box" action="http://127.0.0.1:8081" method='GET'>
        <h1>Login</h1>
        <input type="text" name="text" placeholder="Username">
        <input type="password" name="password" placeholder="Password">
        <input type="submit" value="登录">
        <input type="submit" formaction="http://127.0.0.1:8081/add" value="注册">
    </form>
</body>

</html>
var mysql = require('mysql');      //导入mysql数据库
var express = require('express');  //构建后端接口
var app = express();
//将数据库信息包装方便重复调用
function con() {
    var connection = mysql.createConnection({
        host: '127.0.0.1',   //数据库地址
        user: 'root',   //数据库用户名
        password: '123456',  //数据库密码
        database: 'w1902'  //数据库名
    });
    return connection;
}

app.get('/add', function (req, res) {
    var con1 = con();
    con1.connect();  //启动链接
    let sql = `insert into apple values (?,?)`;
    let contents = [req.query.text, req.query.password]
    con1.query(sql, contents, function (err, result) {
        if (err) {
            res.send("添加失败!!!")
            console.log("数据错误" + err.message);
        }
        else {
            res.send("添加成功!!!" + JSON.stringify(result))
        }
    });
    con1.end(); //关闭链接
});

app.get("/", function (req, res) {
    var con1 = con();
    con1.connect();
    let sql = `select * from apple where username=? or password=?`;
    con1.query(sql, [req.query.text, req.query.password], function (err, result) {
        if (err) {
            res.send("登陆失败!!!")
            console.log("数据错误" + err.message);
        }
        else {
            if (result[0] != undefined && result[0].username == req.query.text && result[0].password == req.query.password) {
                res.send("登陆成功!!!")
            } else {
                res.send("查无此值!!")
            }
        }
    });
    con1.end(); //关闭链接
});

app.listen(8081, function () {
    console.log("http://127.0.0.1:8081");
});

post传参

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>登录动画</title>
    <link rel="stylesheet" href="css/index.css">
</head>

<body>
    <form class="box" action="http://127.0.0.1:8081" method='post'>
        <h1>Login</h1>
        <input type="text" name="text" placeholder="Username">
        <input type="password" name="password" placeholder="Password">
        <input type="submit" value="登录">
        <input type="submit" formaction="http://127.0.0.1:8081/add" value="注册">
    </form>
</body>

</html>
var mysql = require('mysql');
var express = require('express');
var app = express();

function con() {
    var connection = mysql.createConnection({
        host: '127.0.0.1',
        user: 'root',
        password: '123456',
        database: 'w1902'
    });
    return connection;
}

//使用bodyParser模块,接收post参数
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/add', function (req, res) {
    var con1 = con();
    con1.connect();
    let sql = `insert into apple values (?,?)`;
    let contents = [req.body.text, req.body.password]
    con1.query(sql, contents, function (err, result) {
        if (err) {
            res.send("添加失败!!!")
            console.log("数据错误" + err.message);
        }
        else {
            res.send("添加成功!!!" + JSON.stringify(result))
        }
    });
    con1.end();
});

app.post("/", function (req, res) {
    var con1 = con();
    con1.connect();
    let sql = `select * from apple where username=? or password=?`;
    con1.query(sql, [req.body.text, req.body.password], function (err, result) {
        if (err) {
            res.send("登陆失败!!!")
            console.log("数据错误" + err.message);
        }
        else {
            if (result[0] != undefined && result[0].username == req.body.text && result[0].password == req.body.password) {
                res.send("登陆成功!!!")
            } else {
                res.send("查无此值!!!")
            }
        }
    });
    con1.end();
});

app.listen(8081, function () {
    console.log("http://127.0.0.1:8081");
});