【问题标题】:Send data react axios发送数据反应axios
【发布时间】:2020-04-22 12:44:36
【问题描述】:

Send data React to Node

如果用 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


【解决方案1】:

您似乎需要将bodyParser.json() 放入您的中间件中。 例如:

app.use(bodyParser.json());

之后,您可以在控制器中使用以下代码作为您的 HTTP 响应。

res.status(200).json({ results: countValue });

相信你不用JSON.stringify(countValue)

请随时为我们分享更多详细信息 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多