在客户端,我使用jquery来发布数据:
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 中,我 try catch 发布数据:
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 状态,但我无法记录帖子数据,它什么也不返回,并且终端日志未定义
。
最佳答案
您发送的数据不正确。
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: '/endpoint'
});
对于 jQuery,使用
contentType:'application/json'
发送 JSON 数据。
关于node.js - Node.js 表示 :can not get the data posted by jquery ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11671405/