Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native fetch, Network request failed. not using localhost

I have an app, which I'm using fetch to authenticate user. It was working until few days ago and I haven't change anything. Just upgraded from react 0.27 to 0.28, not fetch is not working.

I have searched for almost 2 days and I have read almost all questions in Stack Overflow. Most of users trying to fetch something from localhost, and when they change it to actual IP address, they get it to work. But I'm not fetching anything from localhost, also mine code used to be working.

Here is my code:

fetch('http://somesite.com/app/connect', {       method: 'POST',       headers: {         'Accept': 'application/json',         'Content-Type': 'application/json',         'language':'en-US',         'Authorization': 'Bearer ' + access_token,       },       body: JSON.stringify({         uid: uid,         refresh_token: refresh_token,         token: access_token,         device: device_id,         device_name: device_name,       })     }) .then((response) => response.json()) .then((responseData) => {     console.log(JSON.stringify(responseData.body)) }) .catch((err)=> {   console.log('Some errors occured');   console.log(err); }) .done(); 

I tried to make some new projects, simple, just used a simple fetch example fro tutorials, it gave same error. I tried to open my website which I'm trying to connect to it, through browser in emulator, it works, but it seems through my, app cannot connect to any website/IP. It gives this error in Chrome console:

TypeError: Network request failed     at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:28193:8)     at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:14591:15)     at XMLHttpRequest.setReadyState (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29573:6)     at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29431:6)     at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29506:52     at RCTDeviceEventEmitter.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:13428:23)     at MessageQueue.__callFunction (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11999:23)     at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11906:8     at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11857:1)     at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11905:1) 

Actually I have the same problem as this user here: React-native network request always fails

info.plist from xcode:
info.plist from xcode

like image 706
Ata Mohammadi Avatar asked Jun 28 '16 13:06

Ata Mohammadi


People also ask

How do you solve network request failed in react native?

It's just that simple! Start your app as usual but don't forget to give an IP address and a port, this will help you solve the Network Request Failed error. And on your mobile app, make sure to use the correct URL in your request. Make sure that CORS is enabled on your backend to avoid errors related to CORS.

Why does my phone say network request failed?

Most of time , when network request failed error occurred, clearing the app cache will resolve the issue. So, To fix Instagram network request failed issue, clear Instagram app cache. To clear the Instagram App cache, For Android users, Go to settings >> Find Instagram App >> Tap on clear cache.

What is network request?

A network request is an HTTP request from your mobile app to a server-side application. The iOS Agent detects network requests when the underlying implementation is handled by the NSURLConnection or NSURLSession classes.


1 Answers

You should check out this link: https://github.com/facebook/react-native/issues/8118

Looks like the issue emerged in React Native 0.28. Solution is to "Allow Arbitrary Loads" in the info.plist file found in the ios>build folder that React creates.

If you open the entire ios folder in xcode, then open this info.plist file you can create a new key to Allow Arbitrary Loads and it should fix your issue.

Allow Arbitrary Loads

like image 108
Ahmed Haque Avatar answered Oct 05 '22 08:10

Ahmed Haque