0

I'm working on a react-native-based mobile application and doing some operations with python in the background. I wanted to do both these transactions and connect to the database via Django rest api. But I get connection error. I have used other rest-api and tried it. I also tried the rest api on the postman and it worked smoothly. I tried everything, but I couldn't find a solution. Error screen

local rest url: http://localhost:8000/api/venues/

api page

and fetch code:

componentDidMount() {
    return fetch('http://localhost:8000/api/venues/?format=json')
    .then((response) => response.json())
    .then((responseJson) => {
      console.log(responseJson);

      this.setState({
        isLoading: false,
        dataSource: responseJson,
      }, function(){

      });

    })
    .catch((error) =>{
      console.error(error);
    });
  }

also my django setting: django settings

  • i think you need to allow cors on your server side take a look [here](https://stackoverflow.com/questions/35760943/how-can-i-enable-cors-on-django-rest-framework?answertab=votes#tab-top) – walid sahli Dec 17 '19 at 22:37
  • i tried, after u mentined but it didnt worked. I added my Django Setup as img. – Artun Burak Mecik Dec 17 '19 at 23:55

2 Answers2

0
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
    'http://localhost:3030',
]
CORS_ORIGIN_REGEX_WHITELIST = [
    'http://localhost:3030',
]

add these configurations after adding cors as middleware (change to your port number)

walid sahli
  • 406
  • 3
  • 11
0

https://stackoverflow.com/a/69186898/12497001 provides a very good answer!

As mentioned in the Android Studio documentation, emulators use a special address network (https://developer.android.com/studio/run/emulator-networking.html).

To address requests to your own machine, you can use the address http://10.0.2.2

Art_Arnaud
  • 41
  • 5