How to fix network error in react-native when access localhost api

爱⌒轻易说出口 提交于 2021-02-20 06:50:23

问题


We're trying to connect to an API (.net core 2.2) from react-native (version 0.59), via localhost, both apparently running on the same ip, different ports, react-native on port 8080, and the api on the 44344, the issue happens the moment we fetch the url from react-native

We’ve also tested running the url from Postman and everything seems ok, also from any of the web browser installed (safari/chrome), even we tested the browser INSIDE react-native iOS, and it works.

Any api running outside localhost works perfectly, is the localhost were we failed.

Network request failed.

onerror
    whatwg-fetch.js:504:29
dispatchEvent
    event-target.js:172:43
setReadyState
    XMLHttpRequest.js:580:29
__didCompleteResponse
    XMLHttpRequest.js:394:25
emit
    EventEmitter.js:190:12
__callFunction
    MessageQueue.js:366:47
<unknown>
    MessageQueue.js:106:26
__guard
    MessageQueue.js:314:10
callFunctionReturnFlushedQueue
    MessageQueue.js:105:17
callFunctionReturnFlushedQueue
    [native code]:0

Part of the code (function) that fetch the api, very simple (for now)

async Submit(){
  //GET METHOD
  try {
        let response = await fetch('https://192.168.1.56:44344/api/values');
        let responseJson = await response.json();
        return Alert.alert(JSON.stringify(responseJson));
  } catch (error) {
        console.error(error);
  }
}

Ok first of all, we've tried EVERY possible solution,to connect my react native app to my .net core api rest, both running in localhost, this is the list so far, of the things that we've tried so far, and still no result.

  • Localhost
  • 127.0.0.1
  • Computer ip (network ip not mac address)
  • React Native blank project (from the ground up)
  • API .net core blank project (from the ground up)
  • Running snack expo + api .net core
  • ip forwarding (We can't do that do to our job policies/Not the solution we're looking for)
  • http/https
  • Different ports
  • Android and ios permissions from react-native
  • Same network different ip (this sorta worked, but we don't know exactly why it doesn't work running both react-native and the api in the same ip (localhost))
  • 10.0.2.2 (for android)
  • Enable cors on api .net core (but apparently this doesn't work on native apps, only for web)
  • Expose the ip through ngrok/serveo (We can't do that do to our job policies/Not the solution we're looking for)
  • Frisbee
  • Axios
  • Websocket (We can't do that do to our job policies/Not the solution we're looking for)
  • XMLHttpRequest (status code error 0)
  • Firewall/proxy (our network is free from firewalls and proxies)
  • Web browser plugins (deactivated and/or uninstalled)
  • Cache/cookies
  • Docker (We can't do that do to our job policies/Not the solution we're looking for)
  • Reboot my macbook pro

we expect react native to fetch the api, so we can continue with the office 365 login authentication.

EDIT: I just discovered that fetching the machine ip (this time running windows), with my ip being 192.168.0.9 both on the api and the react native, the fetch result showed me the 10.0.2.2:80 in the header of the json response, which i suppose is the "localhost" ip from react native, how am i suppose to fetch an ip from localhost if react native is not letting me to do so apparently.

EDIT: We had to go for plan B this time around, we make it work with a docker on the api, but i need a solution for this problem, i'm 99% sure the issue is react-native and nothing else

EDIT: SOLVED !!!! after all these weeks one of my colleges manage to solve it, this is how he did it, first of all, the firewall in my macbook pro is a piece of garbage, and we couldn't make it work properly, second, we solved that and found out our api was having issues, so he found out the redirection was on https, and the certifications weren't working properly, so he changed this

"applicationUrl": "http://192.168.0.114:5001;https:192.168.0.114:5001"

to

"applicationUrl": "http://192.168.0.114:5001"

回答1:


I got the same issue while fetching localhost API from react-native. Here is what I did to solve this issue. In the startups class, I removed //app.UseHttpsRedirection(); from Configure method.(Not sure if it is needed)

After that in the launchsetting.js file under the properties folder, I changed from

"applicationUrl": "https://localhost:5001;http://localhost:5000"

to "applicationUrl": "http://localhost:5000"

and in the React native part, I simply changed my localhost with my IP:

fetch('http://localhost:5000/values/') to fetch('http://127.0.0.1:5000/values/')

This completely worked for me




回答2:


Take command prompt and type ipconfig, so we will be getting many addresses. From that take IPV4 and copy it to the clipboard. Then paste this address to both the back ends port address and front end port address. eg say IPV4 address is 192.168.1.3 then make you your back end route API similar to this http://192.168.1.3:8080/patients, where 8080 is the port number (never mind). And use the same for the front end to grab the API result.



来源:https://stackoverflow.com/questions/57099563/how-to-fix-network-error-in-react-native-when-access-localhost-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!