我正在向我的节点/快递服务器发送一个 JSON 结构并将对象保存到数据库中。问题是我发送带有整数和布尔值的 JSON,但所有内容都保存为字符串。
这是我的节点/快递代码:
var express = require('express');
var app = express();
app.enable("jsonp callback");
app.use(express.bodyParser());
// allow cross origin scripting to get data from devices directly
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.post('/departures', function(req, res) {
/* I started using this to convert back to integers - but need to solve the problem
for (var i in req.body.data) {
req.body.data[i].siteid = parseInt(req.body.data[i].siteid);
}
*/
console.log('saving data '+JSON.stringify(req.body.data));
positionProvider.save(req.body.data, function(){
res.json({status:'success'});
})
});
这是我使用 jquery 发布的方式:
var data = [{"siteid":123}];
$.ajax({
type: 'POST',
url: serverUrl + '/departures',
data: {
data: data
},
success: function(resp) {
alert('saved departure data '+JSON.stringify(data))
},
error: function(err) {
console.log('error posting to server...');
console.log(err);
}
});
jquery 端报告它发送了 {"siteid":123},但节点端报告它收到了 {"siteid":"123"}。
整数在哪里转换为字符串?