Jquery Node.js+;快速POST请求返回未定义的

Jquery Node.js+;快速POST请求返回未定义的,jquery,node.js,express,post,body-parser,Jquery,Node.js,Express,Post,Body Parser,我在后端有Node.js+express,并试图解析简单POST请求的主体。 但结果是在服务器控制台上获得“未定义” 我在StackOverflow上尝试了很多我在这里找到的选项-什么都不管用。 (版本中似乎不需要正文解析器,但即使包含正文解析器也不起作用) 下面是我的代码示例: Node.js配置(快速版本:express@4.17.1): var express = require('express'); var app = express(); const path = require('

我在后端有Node.js+express,并试图解析简单POST请求的主体。 但结果是在服务器控制台上获得“未定义”

我在StackOverflow上尝试了很多我在这里找到的选项-什么都不管用。 (版本中似乎不需要正文解析器,但即使包含正文解析器也不起作用)

下面是我的代码示例:

Node.js配置(快速版本:express@4.17.1):

var express = require('express');
var app = express();
const path = require('path');
const PORT = process.env.PORT || 5000;

let ejs = require('ejs');

app.use(express.json());
app.use(express.text());

/* ... Some code ... */
  .use(express.static(path.join(__dirname, 'public')))
  .set('view engine', 'ejs')
  .post('/gaapi-post', function(req, res){
    console.log('***** Post fields: ******');
    console.log(req.body);
  })  

// jQuery JSON
jQuery.ajax({
    url: '/gaapi-post',
    type: 'POST',
    contentType: 'application/json',
    data: { "x": "5", "y": "6" }, // Valid JSOP should have " instead of '
});

// jQuery simple text
$.post( "/gaapi-post", 'test');


// Curl in console
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:5000/gaapi-post

Node.js路由:

var express = require('express');
var app = express();
const path = require('path');
const PORT = process.env.PORT || 5000;

let ejs = require('ejs');

app.use(express.json());
app.use(express.text());

/* ... Some code ... */
  .use(express.static(path.join(__dirname, 'public')))
  .set('view engine', 'ejs')
  .post('/gaapi-post', function(req, res){
    console.log('***** Post fields: ******');
    console.log(req.body);
  })  

// jQuery JSON
jQuery.ajax({
    url: '/gaapi-post',
    type: 'POST',
    contentType: 'application/json',
    data: { "x": "5", "y": "6" }, // Valid JSOP should have " instead of '
});

// jQuery simple text
$.post( "/gaapi-post", 'test');


// Curl in console
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:5000/gaapi-post

POST请求示例:

var express = require('express');
var app = express();
const path = require('path');
const PORT = process.env.PORT || 5000;

let ejs = require('ejs');

app.use(express.json());
app.use(express.text());

/* ... Some code ... */
  .use(express.static(path.join(__dirname, 'public')))
  .set('view engine', 'ejs')
  .post('/gaapi-post', function(req, res){
    console.log('***** Post fields: ******');
    console.log(req.body);
  })  

// jQuery JSON
jQuery.ajax({
    url: '/gaapi-post',
    type: 'POST',
    contentType: 'application/json',
    data: { "x": "5", "y": "6" }, // Valid JSOP should have " instead of '
});

// jQuery simple text
$.post( "/gaapi-post", 'test');


// Curl in console
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:5000/gaapi-post

上述代码中可能会有哪些更改或改进


提前谢谢

默认情况下,req.body是未定义的,并且在使用body解析器和multer等body解析中间件时填充。因此,您需要使用其中一个中间件。Express(4.x)将主体解析器中间件与核心框架分离。因此,如果需要主体解析器,则需要单独安装:

npm install body-parser --save
以下是主体解析器的代码示例:

const app = require('express')();
const bodyParser = require('body-parser');

// for parsing application/json
app.use(bodyParser.json());

// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));