0

I have a very simple React app in which I am trying to make an api call to my Flask API that is hosted on localhost:5000. Here is the fetch method:

   //Fetch all forms from database
    useEffect(() =>{
      setIsLoading(true);
      fetch('/api/forms').then(response =>{
        if(response.ok){
          return response.json();
        }
      }).then(data => console.log(data));
    }, []);

I also added this line to my package.json which a lot of people recommend: "proxy": "http://localhost:5000/",.

I have also tried fetch('http://MY-IPV4-ADDRESS:5000/forms') and fetch('localhost:5000/forms') which did not work.

The error in the browser console simply reads "Failed to load resource: net::ERR_CONNECTION_REFUSED"

If I simply open a web browswer and navigate to localhost:5000/forms I get the proper response including all form data.

I then tried fetch('http://127.0.0.1:5000/forms') which gives me a 200 response from the API which usually means it was successful, but in the browser console says "Access to fetch at 'http://127.0.0.1:5000/forms' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

I also have a React Native app which makes the very same api call like this:

  //Fetch all formsfrom database
  useEffect(() =>{
    setIsLoading(true);
    fetch('http://10.0.2.2:5000/forms').then(response =>{
      if(response.ok){
        return response.json();
      }
    }).then(data => setFullData(data)).then(setIsLoading(false));
  }, []);

Which I have tried to no avail despite that this works fine in the React Native application.

This is the file structure of the project just to give you an idea of where everything is:

enter image description here

What am I doing wrong?

Cole Perry
  • 197
  • 21
  • reasons can be diferent. did you check logs on backend? does api call "comes" there, if yes, how your backend reacts on them? any errors in browser console? – Sam Fisher Jun 09 '21 at 20:23
  • I just realized I kind of left out a bunch of details give me a few and I'll fix the question. I apologize. – Cole Perry Jun 09 '21 at 20:28
  • Ok I added all the info I could which wasnt much. Where exactly do I find "logs on backend"? There are no logs in the React app and the API logs say "GET /forms HTTP/1.1" 200 - which usually means it worked. – Cole Perry Jun 09 '21 at 20:39

3 Answers3

0

Did you try to catch the error?

fetch().then().catch(err => console.log(err))
JustCarty
  • 3,839
  • 5
  • 31
  • 51
devpolo
  • 2,487
  • 3
  • 12
  • 28
0

According to updated information, I suppose that problem can be not in react or fetch. You should fix cors-problem. here you can read about cors and possible solutions or here or may be you should configure flask security.I don't work with pyton, but in spring I had to modify security-config.

Sam Fisher
  • 746
  • 2
  • 10
  • 27
  • So in that case, do you think that the code that I have should work (assuming the address in the fetch is correct) if the cors error was fixed? – Cole Perry Jun 10 '21 at 13:24
0

I figured it out. I downloaded this package and followed the instructions exactly and everything is fine now. https://flask-cors.readthedocs.io/en/latest/

Cole Perry
  • 197
  • 21