4

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>
     )
}
5
  • Are you trying to make a request from your device? Commented Jun 6, 2018 at 16:09
  • on the android emulator Commented Jun 6, 2018 at 16:09
  • Looke that: github.com/facebook/react-native/issues/10404 Commented Jun 6, 2018 at 16:14
  • thanks man, it was problem with localhost Commented Jun 6, 2018 at 16:22
  • you are welcome :D Commented Jun 6, 2018 at 16:24

2 Answers 2

1

Use 10.0.2.2 IP Address instead of localhost.

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

Comments

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

Comments

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.