![](/img/trans.png)
[英]i want to parse this json data with nodejs but gets error undefined
[英]why I'm getting undefined object and How do I parse json data with NodeJs?
我的架構如下:
服務器端是NodeJs
客戶端是HTML&JavaScript&JSON
我正在嘗試將一些數據從客戶端發送到服務器,但是服務器始終會獲取無效數據。
服務器:
app.post('/process_post', urlencodedParser, function (req, res) {
console.log("Got POST (RES): " + res);
console.log("Got POST (RES-BODT): " + res.body);
res.sendFile( __dirname + "/" + "MyWeb.html" );
})
客戶:
function onSubmit() {
var xmlhttp;
var result = '{"Q1":1,"Q2":3,"Q3":9}'
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","~/process_post",true);
xmlhttp.send(JSON.stringify(result));
// same result when sending the next data:
//xmlhttp.send("XYZ");
return true;
}
但是在服務器端,我總是得到:
Got POST (RES): [object Object]
Got POST (RES-BODT): undefined
謝謝
如果使用express,請使用express正文解析器: https : //github.com/expressjs/body-parser
添加限制50mb-用於發送更多日期: app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.