【问题标题】:Server not returning JSON from Express to React (proxy)服务器未将 JSON 从 Express 返回到 React(代理)
【发布时间】:2017-11-28 06:03:05
【问题描述】:

我正在尝试制作一个具有 React 前端(在端口 8080 上运行)和 Express-Node.js 后端(在端口 3000 上)的应用程序。我想让我的客户使用fetch 从我的服务器请求数据。到目前为止,我在网上看到的内容表明我需要在我的package.json 中添加一个proxy 条目,其值为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 node.js reactjs express proxy


    【解决方案1】:

    您需要将json 拉出您的response

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

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2019-03-10
      • 2020-04-02
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多