Node.js:提交表单时接收到空的正文 [英] Node.js: Receiving empty body when submitting form

查看:95
本文介绍了Node.js:提交表单时接收到空的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里已经问了很多类似的问题,但是我仍然无法解决我的问题.我真的不理解发生了什么. 我正在尝试通过post请求从表单发送数据,但始终收到一个空的正文. 表单的Jade代码如下所示:(添加了enctype)

I'm aware that similar questions have been asked a lot here, but I still couldn't solve my problem. I really don't undestand whats going on. I'm trying to send data from a form via post request but keep receiving an empty body. The Jade code for the form looks like this: ( added enctype)

    form(enctype="application/json" action="/test" method="post" name="login")
        table
            tr
                th
                    label(for="username") Username
                td
                    input(type="text" name="username" placeholder="username")
            tr
                th
                    label(for="password") Password
                td
                    input(type="password" name="password" placeholder="password")
            tr
            tr
                th
                td
                    input(type="submit" value="Login")

这会生成以下HTML代码(添加HTML代码):

Which generates the following HTML code ( add HTML code):

<form enctype="application/json" action="/test" method="post" name="login">
  <table>
    <tr>
      <th>
        <label for="username">Username</label>
      </th>
      <td>
        <input type="text" name="username" placeholder="username">
      </td>
    </tr>
    <tr>
      <th>
        <label for="password">Password</label>
      </th>
      <td>
        <input type="password" name="password" placeholder="password">
      </td>
    </tr>
    <tr></tr>
    <tr>
      <th></th>
      <td>
        <input type="submit" value="Login">
      </td>
    </tr>
  </table>
</form>

所以我没有忘记name属性.

这是我处理请求的方式:

This is how I handle the request:

router.post('/test', function(req, res) {
    console.log('body:', req.body);
    res.render('login', {title: 'HK Test'});
});

哪个打印body {}.因此,我确定请求已收到.

Which prints body {}. So I am sure that the request is received.

我也在使用bodyparser:

I am also using bodyparser:

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());`

但是,当我使用curl:curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:4000/login时,主体包含用户名和对象.我从另一个工作项目中复制了Jade代码,所以我真的不明白自己在做什么错.

However, when I use curl: curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:4000/login, the body contains username and objects. I copied the Jade code from another working project, so I really don't understand what I'm doing wrong.

有人知道这可能是什么原因吗?

Does anyone have an idea about what could be the reason for this?

推荐答案

要在提交时将表单数据序列化为JSON,您必须将此属性添加到表单中:

To serialize your form data to JSON when submitting, you have to add this attribute to your form:

enctype="application/json"

否则,在没有JS的任何帮助的情况下,您的表单数据将不会封装为JSON的POST请求.

Otherwise, without any help from JS, your form data will not be encapsulated as a JSON for a POST request.

进一步阅读:

W3C JSON表单提交

这篇关于Node.js:提交表单时接收到空的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆