我将
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',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”}.
整数被转换成字符串在哪里?