我将为您翻译以下内容:
做完这个之后,我能够在Express的req.body中看到它!希望这能帮助到某些人。
我正在尝试使用fetch()方法通过POST方式将JSON发送回我的express应用程序。以下是我的代码:
fetch('https://development.c9users.io/canadmin',{
method:'POST',
body:JSON.stringify({
csv: results
})
这是我在Express中的提交方法:
app.post("/canadmin", function(req,res){
var data = req.body.csv;
console.log(data);
// r.db('cansliming').table('daily').insert( r.json(data) ).run(err, );
res.redirect("canadmin");
});
我在控制台中得到了“未定义”的输出,因此我不确定我是否正确地获取了我的数据。我错过了什么?我的fetch-body有误吗?这就是我的猜测,然而我无法找出正确的语法。
// ***********编辑-解决方案************ //
我去参加了一个Javascript聚会,一个名叫Alex的好人找到了解决方案。标题是不正确的,我们还添加了模式:
fetch('/saveRecords',{
headers: {
//This is the line we need for sending this data
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
mode : 'no-cors',
method:'POST',
//This is the line we need actually send the JSON data
body: JSON.stringify(results)
}) //End fetch()
做完这个之后,我能够在Express的req.body中看到它!希望这能帮助到某些人。
String
是否有特别的理由? - abdulbarik