【发布时间】:2020-04-22 12:44:36
【问题描述】:
如果用 axios 发送数据。
对吗?
反应:
let data = {pp: number};
axios.post('http://localhost:3001/number', {
body: data
}). then((response) => {
console.log('data submitted success');
}).catch((error) => {
console.log('got err', error);
});
服务器中的这个
router.post('/', function (req, res) {
var countValue = req.body;
console.log('CountValue is', countValue);
});
【问题讨论】:
-
看起来您正在发布到名为
/number的路由,并支持名为/的路由,即根路径。路由器是否已经嵌套在number下?这两个必须相同,您的后端才能接收 POST。 -
这能回答你的问题吗? axios post request to send form data
-
“正确”是什么意思?给定的代码有问题吗?
-
我尝试使用 fetch 发送数据。使用此代码,它也可以工作。以及如何从服务器获取响应?
-
嗯,我热烈响应。在服务器 res.end(JSON.stringify(countValue));在前面它返回这个:[object Object] {config:Object {...},data:34,headers:Object {...},request....我需要在变量中分配“data:34”。这是否意味着我应该使用 (bodyParser.json()?
标签: reactjs