Node.js 无法从提取节点读取帖子
我正在将数据Node.js 无法从提取节点读取帖子,node.js,reactjs,redux,fetch-api,Node.js,Reactjs,Redux,Fetch Api,我正在将数据{id:“abc123”,text:“sometext”}发布到一个节点API。使用操作调用从组件过帐: export function addTextToAPI(inputData) { return(dispatch) => { console.log(inputData), dispatch(addText(inputData)) fetch(myapi, { headers: {
{id:“abc123”,text:“sometext”}
发布到一个节点API。使用操作调用从组件过帐:
export function addTextToAPI(inputData) {
return(dispatch) => {
console.log(inputData),
dispatch(addText(inputData))
fetch(myapi, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
data: inputData
})
.then(res => res.json())
}
}
log(inputData)是{id:“abc123”,text:“sometext”}
节点:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/addtest', (req, res) => {
console.log(req.body); // <= returns blank object {}
console.log(req.body.data); // <= returns undefined
console.log(req.query); // <= returns blank object {}
console.log("test added");
});
app.listen(3000);
var express=require('express');
var bodyParser=require('body-parser');
var-app=express();
use(bodyParser.json());
app.post('/addtest',(请求、回复)=>{
console.log(req.body);//发布需要传入body
参数以获取的数据
这应该起作用:
export function addTextToAPI(inputData) {
return(dispatch) => {
console.log(inputData),
dispatch(addText(inputData))
fetch(myapi, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(inputData)
})
.then(res => res.json())
}
}
我还尝试了JSON.stringify(inputdata)
但它不起作用。你的内容类型请求头是什么?@Phix..更新了有问题的获取
。请看一看。传递正文
不起作用。现在我甚至没有从节点获取任何控制台日志
。更改回数据
控制台日志正在起作用。我认为传递正文
不起作用all node at all.tryJSON.stringify
inginputData
并将其作为body进行传递!JSON.stringify
成功了。为什么我需要添加body
,而不能像任何其他参数一样传递它?这是在fetch中,意味着这就是api的设计方式-你必须传递body
。