【问题标题】:problem with html form submit with nodejs使用nodejs提交html表单的问题
【发布时间】:2020-07-07 06:36:24
【问题描述】:

我用node.js + express + SQLite DB创建了一个简单的注册和登录应用程序,我成功地将register.html的用户详细信息推送到SQLite数据库,但卡在了登录页面。我尝试发出一个带有用户名值的简单发布请求,然后将其打印在控制台中进行检查,但是当我单击login.html 上的提交按钮时没有响应。谁能解释这里发生了什么?!

login.html

<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>Sign in</title>
  <link rel="stylesheet" href="stylekit.css">
</head>

<body>
  <div class="login-box">
    <div class="home">
      <a href="index.html"><i class="fa fa-home"></i></a>
    </div>
    <form action="http://127.0.0.1:5500/myaction.html" method="post">
      <h1>Sign in</h1>

      <div class="textbox">
        <i class="fas fa-user"></i>
        <input type="text" placeholder="Username" name="uname">
      </div>

      <div class="textbox">
        <i class="fas fa-lock"></i>
        <input type="password" placeholder="Password" name="pass">
      </div>
      <input type="button" class="btn" value="Sign in">
  </form>
  </div>
</body>
</html>

server.js

var express = require('express');
var app = express();
var sqlite3 = require('sqlite3');
var db = new sqlite3.Database('db/database.db');
var bodyparser = require('body-parser');

app.use(bodyparser.urlencoded({extended:false}));


app.post('/database',function(request,response){
   console.log('POST request received at register/database');
  db.run('INSERT into database values(?,?,?,?)',[request.body.firstname,request.body.lastname,
    request.body.password,request.body.cpass],function(err){
          if(err){
            console.log(err);
          }
          else{
            response.status(200).redirect('http://127.0.0.1:5500/public/login.html');
          }
    });
    response.end();
});

app.post('/myaction', function(req, res) {
  res.send('You sent the name "' + req.body.firstname + '".');
  res.end();
});

app.listen(8080,function(err){
  if(err){
    console.log(err);
  }
 else console.log('server is running');
});

【问题讨论】:

标签: html node.js forms sqlite express


【解决方案1】:
<input type="button" class="btn" value="Sign in">

这不是提交按钮。它是一个默认什么都不做的按钮(设计意图是绑定一个 JS 事件处理程序)。

改用提交按钮。

<input type="submit" value="Sign in">

<button>Sign in</button>

这些不匹配:

action="http://127.0.0.1:5500/myaction.html"

app.post('/myaction',

【讨论】:

  • 天哪!我没注意到,非常感谢兄弟,现在可以使用了!!!
猜你喜欢
  • 2015-07-09
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 2011-02-04
  • 2022-01-16
  • 2012-11-09
  • 2011-06-15
相关资源
最近更新 更多