Reputation: 15656
In client side, I use jquery to post data:
var data = {
'city': 'england'
}
$.ajax({
'type': 'post',
'url': 'http://127.0.0.1:8000/',
'data': data,
'dataType': 'json',
'success': function () {
console.log('success');
}
})
In my server.js, I try to catch the post data:
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);
the post could success, it return 200 status, but I can't log the post data, it return nothing, and the terminal log undefined
.
Why? How can I solve this problem?
Upvotes: 1
Views: 1457
Reputation: 34158
You are not sending the data right.
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: '/endpoint'
});
With jQuery, use
contentType:'application/json'
to send JSON data.
Upvotes: 2