0

I have an expo react native app, and i want to use a function from the API i made, in another project. I tested the url in Postman, and the url works fine. But i get the error: Network error failed in the app.

the klt_id and klt_name is from the Clients table. the url leads to a function that shows all the records from the Clients table.

this is what i have right now:

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>
    );
  }
}

Thanks in advance!

sander106
  • 23
  • 7
  • 1
    Does this answer your question? [Requesting API with fetch in javascript has CORS policy error](https://stackoverflow.com/questions/56379638/requesting-api-with-fetch-in-javascript-has-cors-policy-error). There are gazillions of posts why it didn't work for you. Please read what the [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is. – emix Jan 22 '20 at 08:20
  • what are you using? an emulator or a real device? – LonelyCpp Jan 22 '20 at 08:23
  • I am using a real device – sander106 Jan 22 '20 at 08:29
  • still haven't found a solution yet – sander106 Jan 22 '20 at 11:14

1 Answers1

0

You could give ngrok a try: it'll allow you to have a "normal" HTTPS URL you should be able to use in your Expo app without any issue. Example from their website:

enter image description here

Charles Mangwa
  • 151
  • 2
  • 5