使用Fetch将JSON放入文档体-Javascript
我正在尝试使用fetch()使用POST方法将JSON发送回我的express应用程序。这是我的密码:使用Fetch将JSON放入文档体-Javascript,javascript,node.js,express,fetch,Javascript,Node.js,Express,Fetch,我正在尝试使用fetch()使用POST方法将JSON发送回我的express应用程序。这是我的密码: fetch('https://development.c9users.io/canadmin',{ method:'POST', body:JSON.stringify({ csv: results }) 以下是我在Express中的发布方法: app.p
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");
});
我通过console.log未定义,因此我不确定是否正确获取了数据。我错过了什么?我的取回正文不正确吗?这就是我的猜测,但是我不能找出正确的语法
//***********编辑解决方案************//
我参加了一个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中看到它!我希望这对某人有帮助 通常,您必须像读取流一样读取
req
。大概是这样的:
var body = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
console.log(JSON.parse(body));
});
app.post('/something_url', urlEncode, (req, res) => { ...your code here...});
更好的解决方案是使用。对于post方法,最好使用主体解析器中间件 例如伪代码:(ES6格式) 包括主体解析器:
import bodyParser from 'body-parser';
const urlEncode = bodyParser.urlencoded({extended: false});
现在,将其用于api post请求,如下所示:
var body = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
console.log(JSON.parse(body));
});
app.post('/something_url', urlEncode, (req, res) => { ...your code here...});
我也有同样的问题。如何调用fetch。
我没有接到任何更改电话,因为我不知道它是否有效稍微不同的解决方案对我有效。体分析器 客户端
fetch
call将数据发送到express:
fetch('api/users', {
headers: { 'Content-Type': 'application/json' }, // tells the server we have json
method:'PUT', // can be POST
body: JSON.stringify(results), // json is sent to the server as text
})
服务器端expressapp
侦听application/json
并将其解析为json:
const express = require('express')
const app = express()
// parse application/json
app.use(bodyParser.json())
在设置了bodyParser
之后,我定义了一个express中间件函数
app.put('/api/:collection', (req, res) => {
logger.log('put', req.body)
// ...
})
req.body
是json。console.log(req.body)我这样做了,它刚刚给出了{}有什么具体的原因吗,您在请求期间转换String
中的body吗?我实际上有bodyParser,但我不想使用它。在bodyParser中我该如何做?@illcrx我链接到了说明app.use(bodyParser.json())
@illcrx如果我刚才给你的代码不起作用,你应该发布一个新问题。我认为答案应该与问题匹配,而不是相反。为什么我要回复同样的问题?@illcrx你现在有一个不同的问题。你一开始问如何使用Express处理邮件正文。现在您正在询问如何使用body parser中间件,而我在注释和文档中提供的代码对您不起作用。这是一个完全不同的问题,需要显示不同的代码。问题我使用的是类似的东西,我有这个:。。。。。。。。。var bodyParser=require(“body parser”);。。。。。。。。。。。。。。。。。。。。。。app.post(“/canadmin”,bodyParser,function(req,res){var data=req.body;console.log(data);//r.db('cansliming').table('daily').insert(r.json(data)).run(err,);res.redirect(“canadmin”);};考虑<代码>标题=新标题({内容……‘应用程序/JSON’,……其他标题…})< /代码>我遇到了这个问题,我没有花时间解决的具体原因,但是能够将现有的头属性传递给构造函数,并成功地覆盖原件(到API中)。