Javascript 如何从NodeJ发送数据以作出反应?
这是我的代码,其中包括Js文件和React文件Javascript 如何从NodeJ发送数据以作出反应?,javascript,node.js,reactjs,axios,Javascript,Node.js,Reactjs,Axios,这是我的代码,其中包括Js文件和React文件 // // JS File // ... app.post('/searchResult',function(req,res) { res.send(searchValue); console.log(`----------------------------`); console.log(`Search Result :`+ searchValue); }) ... // // React File // ... axios.post(BASE
//
// 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);
})
...
//
//
//
结果:[对象]
有什么解决方案吗?当您查看响应时,它包含大量内容以及从api端点发送的数据,这些数据是以嵌套对象格式发送给您的响应。因此,您需要指定要访问响应的哪一部分。 尝试使用以下方法:
dataResult = res.data;
OR
dataResult = res.body;
希望这有帮助 如果您只想得到响应,那么应该可以:
axios.post(BASE_URL + '/searchResult').then(({data}) => {
console.log(data);
})
如果以字符串形式返回数据,请尝试:
const result = JSON.parse(data);
在api中返回json。。。。像这样,在react应用程序中返回res.json(searchValue),尝试console.log(res.data)@sathishkumar,我这样说,结果仍然是[object-object]app.post('/searchResult',function(req,res){res.send(searchValue);console.log(
-------------------------------
);console.log(搜索结果:
+searchValue);res.json(searchValue)})@Israelkusayev我尝试console.log(res.data)但它没有显示任何内容。这是否意味着它无法传递值?我尝试res.data和res.body。两者的结果仍然相同,即[object]将其包装在JSON.stringify中,就像这个JSON.stringify(res.data)