var express = require('express');
var router = express.Router();
Route.js:郵來電來訪
router.route('/signup')
.post(function (req, res) {
console.log('post signup called', req.body);
res.json({message: 'signup'});
});
module.exports = router;
的req.body
永遠是不確定的。我能夠在ajax調用中打印它們的控制檯。我不明白req.body
是未定義的。我錯過了什麼?
阿賈克斯後的數據發送,如:
$.ajax({
url: '/signup',
type: 'POST',
data: params,
success: function (res) {
console.log('res', res);
},
error: function (err) {
console.log('err', err);
}
});
服務器JS:已經使用body-parser
這裏
var express = require("express");
var path = require('path');
var app = express();
var mongoose = require('mongoose');
var request = require("request");
var router = require('./app/routes/route.js');
var functions = require('./app/functions/functions.js');
var http = require('http');
var https = require('https');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var model = require('./app/model/model.js');
app.use('/', express.static(__dirname + '/public_html'));
app.use('/', router);
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ limit: '5mb', extended: false }));
你需要使用['body-parser'](https://github.com/expressjs/body-p arser)在你的Express服務器上。 – robertklep
@robertklep我已經在使用body parser – kittu
@robertklep這樣做也是如此 – kittu