简体   繁体   English

req.body为空express js

[英]req.body is empty express js

I have been spending hours trying to figure out why req.body is empty. 我一直在花几个小时试图弄清楚为什么req.body是空的。 I have looked everywhere on stackoverflow and tried everything but no luck. 我在stackoverflow上到处都看过了,尝试了一切,但是没有运气。

I tried setting: 我尝试设置:

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

but it did not change anything 但它并没有改变任何东西

Here is app.js 这是app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var ajax = require('./routes/ajax');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.disable('etag'); //disable cache control

app.use('/', index);
app.use('/ajax', ajax);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;

Now let's have a look at ajax.js 现在让我们看看ajax.js

var express = require('express');
var router = express.Router();
router.post('/1/kyc/form', function (req, res, next) {
    console.log(req.body) //prints {}
});

This is the request done by the client: 这是客户端完成的请求:

在此处输入图片说明

The Content-Type header of your request is invalid: 您的请求的Content-Type标头无效:

Content-Type: application/json;

The trailing semicolon shouldn't be there. 尾随的分号不应该在那里。 So it should be this: 所以应该是这样的:

Content-Type: application/json

FWIW, it's not bodyParser.urlencoded that's being used here; FWIW,这里没有使用bodyParser.urlencoded because the body content is JSON, it's bodyParser.json that handles processing the request body. 因为主体内容是JSON,所以它是用于处理请求主体的bodyParser.json But it's perfectly okay to have both of these body parsers active. 但是同时启用这两个身体解析器是完全可以的。

EDIT : if what the client sends is beyond your control (or it's too much of a hassle to fix it client-side), you can add an additional middleware to Express that will fix the invalid header: 编辑 :如果客户端发送的内容超出了您的控制范围(或者在客户端修复它太麻烦了),则可以向Express添加一个附加的中间件,以修复无效的标头:

app.use(function(req, res, next) {
  if (req.headers['content-type'] === 'application/json;') {
    req.headers['content-type'] = 'application/json';
  }
  next();
});

Make sure that you do this before the line that loads bodyParser.json . 确保加载bodyParser.json的行之前执行此bodyParser.json

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

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