这个JSON结构何时被转换成所有字符串? [英] When is this JSON structure getting converted to all strings?
问题描述
这是我的节点/快递代码:
var express = require('express');
var app = express();
app.enable(jsonp callback);
app.use(express.bodyParser());
//允许跨原始脚本直接从设备获取数据
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){
/ *我开始使用这个来转换成整数 - 但需要解决问题
for(var i in req.body.data){
req.body.data [i] .siteid = parseInt(req.body.data [i] .siteid);
}
* /
console.log('save 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}。
整数转换为字符串的整数是多少?
将数据转换为字符串作为将其从客户端发送到服务器的产品。请记住,客户端和服务器实际上并不是在JSON中进行通信,而是以文本或二进制数据进行通信。服务器(Express?)隐式解释作为字符串发送的数据,如果您包含 content-type:application / json
请求头,则将其转换为JSON。如果您希望数据以特定格式持续存在,您需要明确地键入检查和转换。
TLDR(评论);不要依靠客户端发送有效的数据。在将其保存到数据库之前将其清理。
I am sending a JSON structure to my node/express server and saving the object into a database. The problem is that I send JSON with integers and booleans but everything gets saved as strings.
Here is my node/express code:
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'});
})
});
Here is how I am POSTing with 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);
}
});
The jquery side reports that it sent {"siteid":123} but the node side reports that it received {"siteid":"123"}.
Where is the integer getting converted to a string?
Your data is converted to a string as a product of sending it from client to server. Remember, the client and server are not actually communicating in JSON, they are communicating in text or binary data. The server (Express?) implicitly interprets the data sent as a string, which is converted to JSON if you include the content-type: application/json
request header. You'll need explicitly to type check and convert on the server if you want the data to persist in a specific format.
TLDR (comments); don't rely on the client to send valid data. Clean it before you save it to the database.
这篇关于这个JSON结构何时被转换成所有字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!