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

2
  • i think you need to allow cors on your server side take a look here Commented Dec 17, 2019 at 22:37
  • i tried, after u mentined but it didnt worked. I added my Django Setup as img. Commented Dec 17, 2019 at 23:55

2 Answers 2

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)

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

1 Comment

I tried, but this time the server did not work in localhost. constantly waiting for loading .
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

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.