关于javascript:React-Native fetch,网络请求失败。不使用本地主机 | 珊瑚贝

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


我有一个应用程序,我使用 fetch 来验证用户身份。它一直工作到几天前,我没有改变任何东西。刚刚从 react 0.27 升级到 0.28,not fetch 不起作用。

我已经搜索了将近 2 天,并且几乎阅读了 stackoverflow 中的所有问题。大多数用户试图从 localhost 获取某些内容,当他们将其更改为实际 ip address 时,他们就可以正常工作了。
但我没有从本地主机获取任何东西,我的代码也曾经工作过。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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();

我尝试做一些新项目,简单,只是使用了一个简单的 fetch 示例来教程,它给出了同样的错误。我试图通过模拟器中的浏览器打开我试图连接到它的网站,它可以工作,但似乎通过我的应用程序无法连接到任何网站/IP。它在 chrome 控制台中出现此错误:

1
2
3
4
5
6
7
8
9
10
11
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)

其实我和这个用户有同样的问题:React-native 网络请求总是失败

更新:
来自 xcode

的 info.plist

任何帮助将不胜感激!

  • 您的应用程序的 info.plist 文件中的 NSAppTransportSecurity 下有什么?
  • 感谢您的回复。我已将屏幕截图添加到主帖。你也可以在这里看到它 i.stack.imgur.com/vvqMk.png
  • @Ataomega您是否尝试过重新启动计算机?我遇到了同样的问题,只需简单的重新启动即可解决。
  • 真的我不记得我有多少次重新启动我的电脑。在任何改变之后我做到了。重置模拟器,重新启动计算机…重新安装本机反应和…
  • 3天后,终于通过降级反应原生解决了问题。
  • 面临同样的问题!
  • @Ataomega 天哪,我不会为此降级 :(
  • 安卓呢?我在android中也面临同样的问题。
  • @Ataomega 你降级到什么版本的 RN,我在 RN 0.49.3 的 android 上面临同样的错误
  • 感谢上帝,我降级到 0.45.2,浪费了我一整天的时间看到那些更改 IP 地址的解决方案


你应该看看这个链接:https://github.com/facebook/react-native/issues/8118

看起来问题出现在 React Native 0.28 中。解决方案是在 React 创建的 ios>build 文件夹中的 info.plist 文件中”允许任意加载”。

如果您在 xcode 中打开整个 ios 文件夹,然后打开此 info.plist 文件,您可以创建一个新密钥以允许任意加载,它应该可以解决您的问题。

Allow

  • @Ataomega 你是如何解决这个问题的。我在安卓中也面临同样的情况
  • @Carlos 您在 Android 上解决了这个问题吗?我在 RN 0.35,Android API 21 上遇到了同样的问题
  • @BenYee:我用我的 IP 地址替换了 localhost 并开始正常工作
  • 安卓呢?我在android中也面临同样的问题。
  • 每个人都提到要更改 ip 地址,但是如果我们使用来自服务器的 rest api 并且不知道它的 ip 怎么办。
  • 不能使用 https 服务器的 IP 地址,否则服务器的证书将不起作用(特定于域)。解决方法是什么?不可能是 React-Native 不能用于连接任何远程资源,除非可以使用静态 IP 地址代替域名来获取它们 – 所以必须有一个解决方案。也许是”fetch()”的替代品??
  • 对安卓有什么想法吗?
  • 我已经在 react native 中使用 https 解决了我的 anroid 网络错误问题。请检查我的要点。 gist.github.com/ecdundar/f9207775e4cf22e939f77c5307265529


这个问题是代码试图获取 HTTP 端点而不是 HTTPS,这是不鼓励的。如果您从 localhost 服务器获取,一个简单的解决方案是通过 Ngrok https 端点公开您的 localhost 服务器。

步骤:

  • 从终端/cmd 下载 ngrok 并运行 ngrok http 8081,您应该将 8081 更改为您的 localhost 服务端口。

  • Ngrok 会给你一个 https 转发端点,只需使用它来访问你的 localhost 服务器。


  • 设置 info.plist 对我也不起作用。正在尝试在 iOS 上运行。


    来源:https://www.codenong.com/38077273/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9008.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?