Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

React Native Network request Failed on fetch - Works Local

$
0
0

I am using some code in a React Native component that is a simple fetch passing a parameter to OMDB API. This could be a CORS issue since if I run it in the format below going directly to omdbapi.com it fails always with Network request Failed. This request however works in the Android emulator on the same network.

   // The fetchData function makes an AJAX call to the OMDB API.
   fetchData(movieinput) {
     console.log("In fetch");
       // We pass the movie the user entered in into the URL for the API call.
       fetch('http://www.omdbapi.com/?t='+movieinput+'&y=&plot=short&r=json')
           .then((response) => response.json())
           .then((responseData) => {
               // After the data is recieved, we set this.state.movie to the result of the API call.
               this.setState({
                   movie: responseData,
               });
           })
           .done();
   }

If however I run the same code going to a local URL that wraps the remote request into a localhost request, it works correctly.

   fetchData(movieinput) {
     console.log("In fetch");
       // We pass the movie the user entered in into the URL for the API call.
       fetch('http://localhost:3000/movie/'+movieinput)
           .then((response) => response.json())
           .then((responseData) => {
               // After the data is recieved, we set this.state.movie to the result of the API call.
               this.setState({
                   movie: JSON.parse(responseData),
               });
           })
           .done();
   }

Any ideas?


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>