Syed
Syed

Reputation: 530

React native: TypeError: Network Request Failed

when I try to run the code below in chrome console it works and I get alert in the browser but when I try to run the same code inside react native environment, I get an error instead: "TypeError: Network Request Failed".

makeRequest(){
    fetch('http://localhost:3000/')
    .then(response => response.json())
    .then(data => alert(data))
    .catch(e => alert(e));
}


render() {
     return(
         <View style = {styles.main}>
            {setTimeout(()=>this.makeRequest(),1000);}
         </View>
     )
}

Upvotes: 4

Views: 3592

Answers (2)

Akhila Antony
Akhila Antony

Reputation: 31

return fetch('http://<your ip>')
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson);
  })
  .catch((error) => {
    console.error(error);
  });

Upvotes: 0

Saravana Kumar
Saravana Kumar

Reputation: 3396

Use 10.0.2.2 IP Address instead of localhost.

Upvotes: 1

Related Questions