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

Ata Mohammadi picture Ata Mohammadi · Jun 28, 2016 · Viewed 30.8k times · Source

I have an app, which im using fetch to authenticate user. it was working till 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 stackoverflow. most of users trying to fetch something from localhost, and when they change it to actual ip address , they get it to work. but im not fetching anything from localhost, also mine code used to be working.

here is my code :

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 im 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

UPDATE : info.plist from xcode

any help will be appreciated!

Answer

Ahmed Haque picture Ahmed Haque · Jul 6, 2016

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