2012-07-26 70 views
1

在客户端,我使用jQuery数据发布数据:Node.js的表达:无法获取的jQuery AJAX发布

 var data = { 
      'city': 'england' 
     } 
     $.ajax({ 
      'type': 'post', 
      'url': 'http://127.0.0.1:8000/', 
      'data': data, 
      'dataType': 'json', 
      'success': function() { 
       console.log('success'); 
      } 
     }) 

在我server.js,我试图抓住后的数据:

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); 

后能成功,它返回200个状态,但我无法登录后的数据,它返回任何结果,并且终端日志undefined

为什么?我怎么解决这个问题?

回答

2

您没有发送正确的数据。

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

使用jQuery,使用

的contentType: '应用/ JSON'

发送JSON数据。

+0

不,它仍然不起作用,contentType已经改变为json.But发布的数据仍然不能显示 – hh54188 2012-07-26 15:29:51

+0

你使用的是什么版本的express?我在2.5.8 – Rajat 2012-07-26 17:58:05