gpt4 book ai didi

json - 服务器未将 JSON 从 Express 返回到 React(代理)

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:35 25 4
gpt4 key购买 nike

我正在尝试创建一个具有 React 前端(在端口 8080 上运行)和 Express-Node.js 后端(在端口 3000 上运行)的应用程序。我希望我的客户使用 fetch从我的服务器请求数据。到目前为止,我在网上读到的内容表明我需要添加 proxy进入我的package.json值为http://localhost:3000 。我已经这样做了,我的服务器正确接收了请求,但它的响应不是我所期望的(JSON 对象)。我做错了什么?

//Server
app.get('/search', function(req, res) {
...
//console.log(section) <-- Is the correct value
res.json(section);
})
...
app.listen(3000)


//Client
handleTouchTap() {
fetch('/search?crn=10001').then(function(response) { //<-- Hard-coded for testing
return response; //<-- Does not contain the value of "section" from server
}).then(function(data) {
console.log(data); //<-- Likewise, does not contain the value
});
}

//From package.json
...
"proxy": "http://localhost:3000",
...

最佳答案

您需要拉出 json从你的response中:

fetch('/search?crn=10001')
.then(response => response.json())
.then(section => console.log(section));

关于json - 服务器未将 JSON 从 Express 返回到 React(代理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44742074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com