简体   繁体   English

使用 NODE 的表单 POST 请求为空/未定义

[英]Form POST requests are empty / undefined using NODE

I'm setting up routes in an API and even simple forms do not seem to send any data at all.我在 API 中设置路由,甚至简单的 forms 似乎根本没有发送任何数据。 Here's a simple form html:这是一个简单的形式 html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test form</title>
</head>
<body>

<form action="/comments" method="POST">
    <label for="name">Name:</label><br>
    <input type="text" id="name" name="name" value="John"><br>
    <label for="email">Email:</label><br>
    <input type="email" id="email" name="email" value="Doe"><br>
    <label for="comments">Comments:</label><br>
    <input type="text" id="comments" name="comments" value="Some comments"><br><br>
    <input type="submit" value="Submit">
</form>

</body>
</html>

This is the app.js running in Node:这是在 Node 中运行的 app.js:

const http = require('http');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const morgan = require('morgan')

const hostname = '127.0.0.1';
const port = 3000;

const server = express();
const router = express.Router();

server.use(express.static(__dirname));

server.use(bodyParser.json());
server.use(morgan('dev'));

server.post('/comments', (req, res) => {
  console.log('reached this API call');
  console.log('sending back to homepage');
  console.log('you sent: ' + req.body);
  res.sendFile(path.join(__dirname + '/index.html'));

});

router.get('/', (req, res) => {
  res.sendFile(path.join(__dirname + '/index.html'));
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

This is what I see in the console after I click the Submit button:这是我单击提交按钮后在控制台中看到的内容:

Server running at http://127.0.0.1:3000/服务器运行在http://127.0.0.1:3000/
reached this API call到达此 API 电话
sending back to homepage返回首页
you sent: [object Object]您发送:[对象对象]
POST /comments 200 2.944 ms - 20059 POST /评论 200 2.944 毫秒 - 20059

the [object Object] has no data in it. [object Object] 中没有数据。 I've tried accessing req.body.name and it is undefined.我试过访问 req.body.name 并且它是未定义的。 What am I missing here?我在这里想念什么?

For parsing of application/x-www-form-urlencoded post data, you need to add对于application/x-www-form-urlencoded post 数据的解析,需要添加

server.use(bodyParser.urlencoded({ extended: true }));

For properly logging the full req.body正确记录完整的req.body

const util = require('util');

console.log('you sent: ' + util.inspect(req.body, { showHidden: false, depth: null }));

I found the answer to the issue.我找到了问题的答案。 Vishnu was correct about using the urlencoded method of bodyParser - but the real issue was due to the .htaccess file. Vishnu 关于使用 bodyParser 的 urlencoded 方法是正确的 - 但真正的问题是由于 .htaccess 文件。 I had to add the following for the POST requests to get through:我必须为 POST 请求添加以下内容才能通过:

Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

Without explicitly allowing the header methods, the GoDaddy shared hosting was blocking them.在没有明确允许 header 方法的情况下,GoDaddy 共享主机阻止了它们。

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

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