[英]React Native - fetch not working on iOS simulator (didn't check other)
This looks like the most common question asked on the SO for React.这看起来像是 SO for React 上最常见的问题。 I tried everything given in various questions/answers.
我尝试了各种问题/答案中给出的所有内容。 Either it is outdated, or not working.
它要么过时,要么无法正常工作。
Here is the code这是代码
const Home = () => {
useEffect(() => {
console.log('start');
fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).then((response) => response.json()).then((data) => {
console.log('result: ', data);
}).catch((err) => {
console.log('failed', err);
}).finally(() => {
console.log('Any hope?');
});
});
return (<Text>Lets log the API's for now</Text>);
};
The above code only logs start
in the cli or debug window, nothing else, not even logs written in finally.上面的代码只记录了在cli中
start
或者调试window,没有别的,甚至最终写入的日志都没有。
I'm using xcode 12.0 (12A7209), react 16.13.1, @babel/core 7.12.10.我正在使用 xcode 12.0 (12A7209),反应 16.13.1,@babel/core 7.12.10。
It is not just with fetch, I have already tried using other libs for http in react native like axios , nothing really helps.这不仅仅是 fetch,我已经尝试过使用 http 的其他库来像axios这样的原生反应,没有任何帮助。 They all tend to behave the same.
他们都倾向于表现相同。
I already checked browser by opening a website in the simulator , it has working internet .我已经通过在模拟器中打开一个网站来检查浏览器,它有工作互联网。 I tried replacing API, available free online.
我尝试替换在线免费提供的 API。
BTW this is bare project.顺便说一句,这是一个裸项目。 I tried similar example with expo, it happens to behave the same.
我用 expo 尝试了类似的示例,它的行为恰好相同。
Thanks谢谢
In case someone else needs it.以防其他人需要它。 I solved it by adding
我通过添加解决了它
<key>NSAllowsArbitraryLoads</key>
<true/>
to a Info.plist file which is located inside ios folder in your project.到位于项目中 ios 文件夹内的 Info.plist 文件。
Thus I changed于是我改变了
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
with和
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
I've ran into this issue where XMLHttpRequest never fires its callbacks like onload or onreadystatechange.我遇到了这个问题,XMLHttpRequest 从不触发它的回调,如 onload 或 onreadystatechange。 This caused React-Native's implementation of Fetch to break (because it is effectively a wrapper around XMLHttpRequest).
这导致 React-Native 的 Fetch 实现中断(因为它实际上是 XMLHttpRequest 的包装器)。 I never solved this issue but I worked around it by using a different library to replace the React-Native Global's prototype of Fetch with react-native-fetch-blob.
我从未解决过这个问题,但我通过使用不同的库将 React-Native Global 的 Fetch 原型替换为 react-native-fetch-blob 来解决这个问题。 react-native-fetch-blob is the most well maintained version of a long running replacement for fetch that uses NativeModules and more robust implementation.
react-native-fetch-blob 是使用 NativeModules 和更健壮实现的 fetch 长期运行替代品中维护得最好的版本。
So for those of you out there searching, there could be a variety of issues.所以对于那些在那里搜索的人来说,可能会有各种各样的问题。 Android of certain versions requires you to declare whether clearText (unencrpyted) is fair to send over the internet.
某些版本的 Android 要求您声明 clearText(未加密)是否可以通过 Internet 发送。 Some iOS versions have bugged out on SSL Cert issues, even while valid certs.
一些 iOS 版本已经在 SSL 证书问题上出现问题,即使是有效证书也是如此。 It's an uncommon bug but there records of issues with RN's fetch going back to 2015 that are similar to the issues in this post.
这是一个不常见的错误,但 RN 的 fetch 问题记录可以追溯到 2015 年,与本文中的问题类似。 Again my solution was to replace Fetch totally, and replace it with react-native-fetch-blob which has a native implementation of the Fetch API.
同样,我的解决方案是完全替换 Fetch,并用 react-native-fetch-blob 替换它,它具有 Fetch API 的本机实现。 There is sometimes also an issue in Metro where if you turn off inlineRequires fetch will sometimes begin to work.
Metro 中有时还会出现一个问题,如果您关闭 inlineRequires fetch 有时会开始工作。 Forget all this and replace fetch.
忘记这一切并替换 fetch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.