【问题标题】:Invoke REST endpoint in ReactJS在 ReactJS 中调用 REST 端点
【发布时间】:2020-06-23 09:39:20
【问题描述】:

我有一个 REST 端点 URL,需要从 ReactJs 主页调用。

ReactJS 代码:

function Home() {
  return ( <div> Welcome to the Home Page!</div> );
}

REST 网址

http://:8080/kie-server/services/rest/server/containers/1_1.0.0-SNAPSHOT`

【问题讨论】:

  • 您可以使用一些库来与 REST 端点(如 axios、requests、needle 等)进行通信。只需阅读他们的文档,您就会找到一种方法 :)

标签: reactjs rest request


【解决方案1】:

您需要使用useEffectfetch 调用API

function Home() {
      useEffect(() => {
         fetch('http://:8080/kie-server/services/rest/server/containers/1_1.0.0-SNAPSHOT`').then(res => res.json()).then(res => // do something with res)
       })
      return ( <div>
          Welcome to the Home Page!
        </div>
      );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-26
    • 2017-11-30
    • 2018-02-28
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多