【问题标题】:React native fetch a local APIReact Native 获取本地 API
【发布时间】:2020-05-08 08:25:41
【问题描述】:

我有一个 expo react 本机应用程序,我想在另一个项目中使用我制作的 API 中的一个函数。我在 Postman 中测试了 url,并且 url 工作正常。但我收到错误:应用程序中的Network error failed。

klt_id 和 klt_name 来自 Clients 表。该 url 指向一个函数,该函数显示 Clients 表中的所有记录。

这就是我现在拥有的:

constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
    return fetch('http://127.0.0.1:8000/api/v2/klant', )
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(responseJson)
        this.setState({
          isLoading: false,
          dataSource: responseJson.data,
        }, function(){

        });

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


  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        <FlatList
          data={this.state.dataSource}
          renderItem={({item}) => <Text>{item.klt_id}, {item.klt_name}</Text>}
          keyExtractor={({id}, index) => id}
        />
      </View>
    );
  }
}

提前致谢!

【问题讨论】:

标签: api react-native expo


【解决方案1】:

您可以尝试ngrok:它可以让您拥有一个“正常”的 HTTPS URL,您应该可以在您的 Expo 应用程序中毫无问题地使用它。他们网站上的例子:

【讨论】:

  • 我试过了,但没有结果。不确定我是否正确,但我收到网关错误
  • 您是否使用他们提供的https 链接(不是您的sn-p 中的http)?
  • 是的,我用过 https
  • 尝试使用您收到的确切错误消息更新您的帖子!它可能会激励一些人
  • 获取错误或ngrok错误
猜你喜欢
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 2021-12-10
  • 2018-06-12
  • 1970-01-01
  • 2020-09-16
相关资源
最近更新 更多