[英]React-native - iOS - Cannot fetch data from localhost
i have the problem, that i can not do a fetch request to localhost api.我有一个问题,我无法对 localhost api 执行获取请求。 If a have my business logic on a server, everything works.
如果在服务器上有我的业务逻辑,那么一切正常。
I use iOS and react-native 0.68.2.我使用 iOS 和 react-native 0.68.2。
If i run my app with xCode a get the following error:如果我使用 xCode 运行我的应用程序,则会出现以下错误:
2022-06-24 19:24:35.088109+0200 MyApp[46047:7055248] Task <409DD042-5979-
4A48-8781-C1E90F7AC8A4>.<4> finished with error [-1004] Error Domain=NSURLErrorDomain
Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61,
NSUnderlyingError=0x280e62160 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "
(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: lo0,
_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}},
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <409DD042-5979-4A48-8781-
C1E90F7AC8A4>.<4>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <409DD042-5979-4A48-8781-C1E90F7AC8A4>.<4>"
), NSLocalizedDescription=Could not connect to the server.,
NSErrorFailingURLStringKey=http://localhost/MyApp/business/public/api/get-offerings,
NSErrorFailingURLKey=http://localhost/MyApp/business/public/api/get-offerings,
_kCFStreamErrorDomainKey=1}
flipper: Desktop failed to provide certificates. Error from flipper desktop:
rsocket::StreamInterruptedException: connection error
I already add the following in my Info.plist, but i get after this an error我已经在我的 Info.plist 中添加了以下内容,但是在此之后我得到了一个错误
<key>NSAllowsLocalNetworking</key>
<true>
Error:错误:
The operation couldn’t be completed
Can someone help me why i cant do a request to localhost?有人可以帮我为什么我不能向 localhost 发出请求吗? I use XAMPP and the server is running.
我使用 XAMPP 并且服务器正在运行。 If i use PostMan to Test my request, everything works with the given Error (URL is included in the error).
如果我使用 PostMan 来测试我的请求,那么一切都适用于给定的错误(URL 包含在错误中)。 Should i use a IP?
我应该使用IP吗? If i fetch to a server, everything works with Postman and request in the app.
如果我获取到服务器,一切都适用于邮递员和应用程序中的请求。 If i use instance of "localhost", "127.0.0.1" i also get the error "network request failed"
如果我使用“localhost”、“127.0.0.1”的实例,我也会收到错误“网络请求失败”
Open your Xcode project and under info tab add these settings..打开您的 Xcode 项目并在信息选项卡下添加这些设置..
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
It should work.它应该工作。
The solution of this problem was not to use "localhost".这个问题的解决方案是不使用“localhost”。 I used my IP from the network and it works.
我使用了来自网络的 IP,它可以正常工作。
I got my solution from this post React Native fetch() Network Request Failed我从这篇文章React Native fetch() Network Request Failed得到了我的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.