Linnot
Linnot

Reputation: 29

fetching data from server running on local computer

I have the following problem. I am fetching the data by making an api request to server running on my local computer. Server is written in ASP.NET. I know that when i have to make a request to localhost when i am using an emulator I should put my Ip Address to succesfully call my local computer instead of emulator. Even when i changed to my proper ip address i still have erorr 'Network Request failed'

This is the code where i fetch the data

componentDidMount() {
        fetch('http://IpAdress:54372/api/cars')
             .then((response) => response.json())
            .then((responseJson) => this.setState({cars: responseJson}))
            .catch((error) => {
            console.error(error);
        })
        ;
    }

package.json

"react": "^16.3.0-alpha.2",
"react-native": "0.55.3",

Upvotes: 0

Views: 1591

Answers (2)

Henning Hall
Henning Hall

Reputation: 1457

Add your ip address to Info.plist in the same way as localhost is listed:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
        <key>your.ip.adress.here</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

Upvotes: 0

user4571931
user4571931

Reputation:

Connect via Wi-Fi

You can also connect to the development server over Wi-Fi. You'll first need to install the app on your device using a USB cable, but once that has been done you can debug wirelessly by following these instructions.

You'll need your development machine's current IP address before proceeding.

Open a terminal and type /sbin/ifconfig to find your machine's IP address.

Make sure your laptop and your phone are on the same Wi-Fi network.

Open your React Native app on your device. You'll see a red screen with an error. This is OK. The following steps will fix that. Open the in-app Developer menu. Go to Dev Settings → Debug server host for device. Type in your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081). Go back to the Developer menu and select Reload JS.

You can now enable Live reloading from the Developer menu. Your app will reload whenever your JavaScript code has changed.

Upvotes: 1

Related Questions