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!

4
  • 1
    Does this answer your question? 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 is. Commented Jan 22, 2020 at 8:20
  • what are you using? an emulator or a real device? Commented Jan 22, 2020 at 8:23
  • I am using a real device Commented Jan 22, 2020 at 8:29
  • still haven't found a solution yet Commented Jan 22, 2020 at 11:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

i have tried it, but no result. not sure if i am doing it correct but i get gateway error
are you using the https link they provided (not the http as in your snippet)?
yea i used https
try to update your post with the exact error message you're getting! It might inspire some people
the error from the fetch or the error from the ngrok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.