[英]ExpressJs | req.body is not empty, but can't acces data
I'm posting some data from my ReactJS client to the Express server, but it seems that I cannot access the data in req.body.... I'm sending a JSON object this way: 我正在将一些数据从我的ReactJS客户端发布到Express服务器,但是似乎我无法访问req.body中的数据。...我正在以这种方式发送JSON对象:
var url = 'http://localhost:8000/scrape/';
const {main} = getState()
const info = { name : main.title, items : main.list}
var options = {
url: url,
method: 'POST',
body : JSON.stringify(info),
json: true,
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
Request(options, (error, response, data) => {
// !error ? dispatch( setSearchResults(JSON.parse(data) ) ) : dispatch( setError(error) );
});
and when I console.log(req.body) in Express, I receive this: 当我在Express中使用console.log(req.body)时,我收到以下消息:
{ '{"name":"test","items":': { '{"url":"test","code":"ING"},{"url":"test","code":"KIN"},{"url":"test","code":"GAO"}': '' } }
In my Express I use bodyParser 在我的Express中,我使用bodyParser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
I tried using JSON.stringify(req.body) and JSON.parse(req.body) but both had not succes, any suggestions? 我尝试使用JSON.stringify(req.body)和JSON.parse(req.body),但是都没有成功,有什么建议吗?
This was the solution: How to allow CORS? 解决方法是: 如何允许CORS?
And this is how I make a correct JSON request: 这就是我提出正确的JSON请求的方式:
var options = {
uri: url,
method: 'POST',
json: {
"name": main.tile, "items" : main.list
}
};
Request(options, (error, response, data) => {
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.