在使用Vue.js构建一个数据后用Vue-resource使用post方法传送出去。结果数据却从一个对象里对象的数组变成了对象里的数组。
这是在Vue-devtools看到的数据原型。
save: function () {
var quesData=this.quesData;
var quesDataNow={
time:Date(),
data:quesData
}
quesDataNow = JSON.stringify(quesDataNow);
this.$http.post('/ques', quesDataNow).then(function(res) {
if(res.body==1) alert("OK");
else alert(res);
});
}
这里用Save方法获取了原型的Data里的数据然后发送到express端。
可是打印出来的时候却从对象变成了数组。并且req.body.data值为undefind
option
請加上 emulateJSON: true
,送出的請求 header
才會是 application/x-www-form-urlencoded
,這樣 body-parser
就不會當成表單解析了。
this.$http.post('/ques', quesDataNow, { emulateJSON: true }).then(function(res) {
if(res.body==1) alert("OK");
else alert(res);
});
}
// 假設情況
app.post('/', function(request, response){
console.log(request.body); // <-- json
});
应该是expresss使用了body-parser这个中间件。把data里面的对象全部解析了,另外就是req.body是查询form提交的表单的参数,楼主console一下req.query.data[0]"[question]"这两个引号请忽略,因为提交出来markdown的格式不对