繁体   English   中英

如何配置app.json以接受所有连接类型,例如HTTP请求?

[英]How can I configure app.json to accept all connection types such as HTTP requests?

在我的Expo React Native应用程序中,我一直试图从Ruby on Rails API中获取数据,但似乎不允许http连接。

我找到的所有解决方案要么将配置添加到Android Info.plist AndroidManifest.xml中,要么将iOS中的Info.plist添加到我无法访问的配置中,但是我宁愿将其保留在Expo中而不是退出。 我试图改变localhost127.0.0.110.0.2.2 ,和我的机器的IP为好,但没有奏效。 我看过有关使用app.json网络进行配置的文档,但找不到有关在Expo中配置App Transport Security的任何信息。 我目前正在android 9.0 Pie上运行Expo。

fetch(`http://localhost:3000/api/v1/trainers`, {
    headers: { 
    Accept: 'application/json',
    "Content-Type": "application/json" 
},
}).then(res => res.json())
.catch(err => console.log(err));
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (420454d9-7271-44d5-b…-3a968d130699:48065)
    at XMLHttpRequest.dispatchEvent (420454d9-7271-44d5-b…-3a968d130699:53513)
    at XMLHttpRequest.setReadyState (420454d9-7271-44d5-b…-3a968d130699:52368)
    at XMLHttpRequest.__didCompleteResponse (420454d9-7271-44d5-b…-3a968d130699:52195)
    at 420454d9-7271-44d5-b…-3a968d130699:52305
    at RCTDeviceEventEmitter.emit (420454d9-7271-44d5-b…-3a968d130699:22482)
    at MessageQueue.__callFunction (420454d9-7271-44d5-b…-3a968d130699:22097)
    at 420454d9-7271-44d5-b…-3a968d130699:21854
    at MessageQueue.__guard (420454d9-7271-44d5-b…-3a968d130699:22051)
    at MessageQueue.callFunctionReturnFlushedQueue (420454d9-7271-44d5-b…-3a968d130699:21853)

您的REST API在哪个域上? 从反应本机应用程序(在模拟器上运行)获取到node.js / express REST api时,我遇到了同样的问题。 我在React中找不到解决方法。

我找到了ngrok( https://ngrok.com )解决方案。 它允许您向外部公开特定的本地域:端口。 通过这种方法,我能够从我的应用程序获取我的REST api。

需要明确的是,我不确定问题是否在React-Native的一端。 我怀疑是我的防火墙不允许连接通过。 我没有使用所有这些设置,而是使用了ngrok。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM