node.js express:无法获取jquery ajax发布的数据

node.js express:无法获取jquery ajax发布的数据,node.js,post,express,Node.js,Post,Express,在客户端,我使用jquery发布数据: var data = { 'city': 'england' } $.ajax({ 'type': 'post', 'url': 'http://127.0.0.1:8000/', 'data': data, 'dataType': 'json', 'success'

在客户端,我使用jquery发布数据:

        var data = {
            'city': 'england'
        }
        $.ajax({
            'type': 'post',
            'url': 'http://127.0.0.1:8000/',
            'data': data,
            'dataType': 'json',
            'success': function () {
                console.log('success');
            }
        })
var express = require('express');
var app = express.createServer();
app.configure(function () {
    app.use(express.static(__dirname + '/static'));
    app.use(express.bodyParser());
})

app.get('/', function(req, res){
    res.send('Hello World');
});

app.post('/', function(req, res){
    console.log(req.body);
    res.send(req.body);
});
app.listen(8000);
在我的server.js中,我尝试捕获post数据:

        var data = {
            'city': 'england'
        }
        $.ajax({
            'type': 'post',
            'url': 'http://127.0.0.1:8000/',
            'data': data,
            'dataType': 'json',
            'success': function () {
                console.log('success');
            }
        })
var express = require('express');
var app = express.createServer();
app.configure(function () {
    app.use(express.static(__dirname + '/static'));
    app.use(express.bodyParser());
})

app.get('/', function(req, res){
    res.send('Hello World');
});

app.post('/', function(req, res){
    console.log(req.body);
    res.send(req.body);
});
app.listen(8000);
post可能成功,它返回200状态,但我无法记录post数据,它不返回任何内容,并且终端日志
未定义


为什么??如何解决此问题?

您没有正确发送数据

$.ajax({
  type: 'POST',
  data: JSON.stringify(data),
  contentType: 'application/json',
  url: '/endpoint'
});
对于jQuery,使用

contentType:'application/json'


发送JSON数据。

不,它仍然不起作用,contentType已更改为JSON。但发布的数据仍然无法显示您使用的是哪个版本的express?我在2.5.8上