简体   繁体   English

无法使用node.js v0.12.7检索POST数据,表示为4,正文解析器,引导程序3

[英]Can't retrieve POST data using node.js v0.12.7, express 4, body-parser, bootstrap 3

I've done this dozens of times in previous apps but for some reason can't get it working any more. 我在以前的应用程序中已经做过数十次,但是由于某种原因无法使其正常运行。 Not sure if it's a version issue or not. 不确定是否是版本问题。 I've looked at every example I could find online and none of it is working. 我看了所有可以在网上找到的示例,但都无济于事。 Not sure what I'm doing wrong. 不知道我在做什么错。 Relevant code: 相关代码:

index.html (copied from bootstrap site - I only added the role, action, and method attrs to the form tag) index.html (从引导站点复制-我仅将角色,操作和方法属性添加到form标记中)

<html>

  <head>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  </head>

  <body>
    <form role="form" action="/form" method="post">
      <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
    </form>
  </body>

</html>

server.js server.js

var express = require("express");
var app = express();
var bp = require("body-parser");
var path = require("path");

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

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

app.post("/form", function(req, res) {
  console.log(req.body);
  res.end();
});

var server = app.listen(4000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});

package.json 的package.json

{
  "name": "test-app",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.13.3",
    "express": "^4.13.3",
    "sqlite3": "^3.0.10"
  }
}

When the form route gets called, req.body is always {} . 调用表单路由时, req.body始终为{} This same setup has worked for me every time in the past though. 过去,每次都使用相同的设置。

Your form input elements are missing name attributes and are therefore not being included with the POST request. 您的表单input元素缺少name属性,因此不包含在POST请求中。 See this answer . 看到这个答案

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

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