2

I'm building a React Native app with Expo, I'm trying to fetch my back-end server like this:

  useEffect(() => {

    fetch('http://192.168.1.89:8080/api/ciborg/groups')
      .then((response) => response.json())
      .then((json) => setData(json))
      .catch((error) => console.log("->>>>"+error))
      .finally(() => setLoading(false));
  });

But is always gives me the following error

"TypeError: Failed to fetch".

I have already tried the following:

fetch('http://192.168.1.89:8080/api/ciborg/groups')
fetch('http://localhost:8080/api/ciborg/groups')
fetch('http://127.0.0.1:8080/api/ciborg/groups')

But it's always the same result. However fetching outside APIs like: https://reactnative.dev/movies.json works.

Can anyone help?

3
  • can you confirm that your server is up and running in port 8080 ? Commented Apr 12, 2020 at 19:22
  • Can you try a fetch with POSTMAN ? Commented Apr 12, 2020 at 20:03
  • (nodejs server)The code has: app.listen(8080, function() { console.log('Listening on port: 8080'); }); And in the console: Listening on port: 8080 The back-end recognises a "GET" requests, for example, but in the front-end it always catch and error. And i can test well the back-end with postman requests and even if i, in the back-end (nodejs) side, create a web browser app to see the request i also can. Commented Apr 13, 2020 at 12:02

1 Answer 1

1

I had a nodejs express server. And i added:

install-> npm install cors
Add to code: 
var cors = require('cors');
app.use(cors());

And it resolved my problem.

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

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.