【问题标题】:req.body is empty in POST express nodejsPOST express nodejs中的req.body为空
【发布时间】:2018-04-30 03:09:51
【问题描述】:

下面是我在服务器端发布数据的 HTML 表单代码。

HTML 代码:

<form action="/Device_Data" method="post">
    <select name="deviceSelect" id="deviceSelect" class="selectdevice" type = "text" style="margin-top:1.5%">
        <option value="" selected="selected">Choose Device</option>
        <option value="Device1">Device 1</option>
        <option value="Device2">Device 2</option>
        <option value="Device3">Device 3</option>
        <option value="Device4">Device 4</option>
        <option value="Device5">Device 5</option>
        <option value="Device6">Device 6</option>
        <option value="Device7">Device 7</option>
        <option value="Device8">Device 8</option>
    </select>
    <button id="Import" value="Import" class="button1" type="button" formenctype="text/plain">Import</button>
</form>

服务器代码:

var express = require('express');
//require the body-parser nodejs module
var bodyParser = require('body-parser');
var path = require('path');
var qs = require('querystring');
var app = express();
// var server = http.createServer(app);

//support parsing of application/json type post data
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: false }));

app.post("/Device_Data", function (req, res) {

  // result.entries contains entities matching the query
 //res.setHeader('Content-Type', 'application/json');
  console.log(req.body);
  res.send(200, req.body);
});

输出:

{}
Thu, 16 Nov 2017 14:05:13 GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at server.js:122:7

谁能帮帮我? 提前致谢。

我只想打印 req.body 数据。

【问题讨论】:

  • 如何提交表单数据?提交按钮在哪里?
  • 提交数据

标签: jquery node.js express post body-parser


【解决方案1】:

将按钮的type 更改为提交,并从此按钮中删除formenctype

 <button id="Import" value="Import" class="button1" type="submit">Import</button>

【讨论】:

  • 不客气,顺便说一下res.status(200).send(req.body);,正如Boratzan所说。并接受我的答案作为检查xd
【解决方案2】:

正如错误所说,不推荐使用语法res.send(status, body):

更改以下代码:

res.send(200, req.body);

收件人:

res.status(200).send(req.body);

有关详细信息,请参阅: http://expressjs.com/en/api.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2019-02-08
    • 2016-02-15
    • 2020-12-17
    相关资源
    最近更新 更多