如何将数据从NodeJS发送到React?

问题描述 投票:-1回答:2

这是我的代码,它由Js File和React File组成。

//
// JS File 
//
...
app.post('/searchResult',function(req,res)
{
res.send(searchValue);
console.log(`----------------------------`);
console.log(`Search Result :`+ searchValue);
})
...
//
// React File
//
...
axios.post(BASE_URL + '/searchResult')
.then(res => 
{
dataResult = res;
console.log(`The result :`+ dataResult);
})
...
//
//
//

结果:[对象]

有解决方案吗

javascript node.js reactjs axios
2个回答
0
投票

当您查看响应时,它包含许多内容以及您从api端点发送的数据,作为以嵌套对象格式提供给您的响应。因此,您需要指定要访问的响应的哪个部分。尝试使用以下内容:

dataResult = res.data;
OR
dataResult = res.body;

希望这可以帮助!


0
投票

如果您只想获得响应,这应该有效:

    axios.post(BASE_URL + '/searchResult').then(({data}) => {
        console.log(data);
    })

如果您以字符串形式返回数据,请尝试:

const result = JSON.parse(data);
© www.soinside.com 2019 - 2024. All rights reserved.