[英]React-Native fetch, Network request failed. not using localhost
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.
刚刚从0.27升级到0.28,没有提取不起作用。
i have searched for almost 2 days and i have read almost all questions in stackoverflow. 我搜索了将近2天,我已经阅读了stackoverflow中的几乎所有问题。 most of users trying to fetch something from localhost, and when they change it to actual ip address , they get it to work.
大多数用户试图从localhost获取内容,当他们将其更改为实际的IP地址时,他们就可以使用它。 but im not fetching anything from localhost, also mine code used to be working.
但我没有从localhost获取任何东西,也是我的代码以前工作。
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. 我试图制作一些新项目,简单,只是使用一个简单的fetch示例来教程,它给出了同样的错误。 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.
我试图打开我的网站,我试图连接到它,通过模拟器中的浏览器,它的工作原理,但似乎通过我的应用程序无法连接到任何网站/ IP。 it gives this error in chrome console :
它在chrome控制台中出现此错误:
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 实际上我在这里遇到与此用户相同的问题: React-native网络请求总是失败
UPDATE : info.plist from xcode 更新: 来自xcode的info.plist
any help will be appreciated! 任何帮助将不胜感激!
You should check out this link: https://github.com/facebook/react-native/issues/8118 你应该看看这个链接: https : //github.com/facebook/react-native/issues/8118
Looks like the issue emerged in React Native 0.28. 看起来问题出现在React Native 0.28中。 Solution is to "Allow Arbitrary Loads" in the info.plist file found in the ios>build folder that React creates.
解决方案是在React创建的ios> build文件夹中的info.plist文件中“允许任意加载”。
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. 如果您在xcode中打开整个ios文件夹,然后打开此info.plist文件,您可以创建一个允许任意负载的新密钥,它应该解决您的问题。
Set the info.plist doesn't work for me too. 设置info.plist对我也不起作用。 Trying to run on iOS.
试图在iOS上运行。
This issue is the code is trying to fetch HTTP endpoint instead of HTTPS, which is not encouraged. 此问题是代码尝试获取HTTP端点而不是HTTPS,这是不鼓励的。 If you are fetching from localhost server, an easy solution will be exposing your localhost server through a Ngrok https endpoint.
如果从localhost服务器获取,一个简单的解决方案是通过Ngrok https端点公开localhost服务器。
Steps: 脚步:
Download ngrok and run ngrok http 8081
from terminal/cmd, where you should change 8081 to your localhost service port. 下载ngrok并从terminal / cmd运行
ngrok http 8081
,您应该将8081更改为localhost服务端口。
Ngrok will give you a https forwarding endpoint, simply use that to access your localhost server. Ngrok将为您提供一个https转发端点,只需使用它来访问您的localhost服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.