首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react native中从Spring boot获取数据?

如何在react native中从Spring boot获取数据?
EN

Stack Overflow用户
提问于 2020-04-29 14:31:19
回答 2查看 678关注 0票数 0

我有一个用react native expo创建的前端,我的后端是用spring boot完成的,模型在google云中当我在本地运行spring boot并在浏览器中输入URL (没有部署'localhost URL‘)时,它会在浏览器上显示数据。我如何才能在不部署google云的情况下将这些数据放到我的react原生应用程序中。

代码语言:javascript
复制
 componentDidMount() {
     fetch('http://192.168.56.1:8080/users/toFrontend?email=meNewTwo@gmail.com')
       .then((response) => response.json())
       .then((json) => {
         this.setState({ data: json});
     })
      .catch((error) => console.error(error))
     .finally(() => {
       this.setState({ isLoading: false });
     });

这是我使用的代码

EN

回答 2

Stack Overflow用户

发布于 2020-04-29 14:51:48

代码语言:javascript
复制
fetch('http://{your IP}:8081/')
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({ message : responseJson.message.data })
        })
      .catch((error) => {
        console.error(error);
      }); 

现在我们有了命令提示符,让我们输入ipconfig,按enter,然后在IPv4 Address下面抓取数字。此号码为您的本端IP。

REST API

代码语言:javascript
复制
fetch('http://{your IP}:8081/',{
          method: 'GET',// denpends upon your call POST or GET
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          }
        })
    .then((response) => response.json())
    .then((responseJson) => {

       console.log(responseJson);

    })
    .catch((error) =>{
        console.error(error);
    });
}

这是官方的link

票数 0
EN

Stack Overflow用户

发布于 2020-04-29 18:41:32

谢谢你们,终于修复了代码中缺少返回的问题

代码语言:javascript
复制
 fetch('URL')
  .then((response) => {
    return response.json();
   })
  .then((responseData => {
    console.log(responseData);
   this.setState({ data: responseData });
     }))

URL的本地主机部分是IPv4,本地Ip和变量是data:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61495560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档