HTTP fetch请求在本机上始终失败
我的服务器上有一个未配置https的后端。在此之前,它只使用http,一切正常。出于某种原因,我现在遇到了以下错误:HTTP fetch请求在本机上始终失败,http,react-native,request,fetch,Http,React Native,Request,Fetch,我的服务器上有一个未配置https的后端。在此之前,它只使用http,一切正常。出于某种原因,我现在遇到了以下错误: TypeError: Network request failed at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504) at XMLHttpRequest.dispatchEvent (event-target.js:172) at XMLHttpRequest.setReadyState (XMLHtt
TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:580)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394)
at XMLHttpRequest.js:507
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:366)
at MessageQueue.js:106
at MessageQueue.__guard (MessageQueue.js:314)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)
我在谷歌上搜索了一下,看起来问题出在http上。它可以在其他API上使用https。但是我的没有配置http。现在有没有办法让它只使用http?如果你在Android上测试,你需要启用
clearTextTraffic
,这样系统就允许你的应用程序通过http进行通信
在您的
AndroidManifest.xml
addandroid:usesCleartextTraffic=“true”
中,如果您在android上进行测试,您需要启用clearTextTraffic
,以便系统允许您的应用程序通过HTTP进行通信
在您的
AndroidManifest.xml
中添加android:usesCleartextTraffic=“true”
好的。有了romin21的提示,我现在就开始工作了。有一个文件
android/app/src/debug/res/xml/react\u native\u config.xml
这会覆盖你的AndroidManifest。所以一开始我试着将它添加到AndroidManifest中,当然它不起作用,我已经让这个文件覆盖了它。但当我将后端的ip地址添加到该文件时,它工作得非常好
<domain includeSubdomains="false">YOUR IP</domain>
您的IP地址
好的。有了romin21的提示,我现在就开始工作了。有一个文件
android/app/src/debug/res/xml/react\u native\u config.xml
这会覆盖你的AndroidManifest。所以一开始我试着将它添加到AndroidManifest中,当然它不起作用,我已经让这个文件覆盖了它。但当我将后端的ip地址添加到该文件时,它工作得非常好
<domain includeSubdomains="false">YOUR IP</domain>
您的IP地址
解决方案之一是将以下代码添加到AndroidManifest.xml
文件中
android:usesCleartextTraffic="true"
但在我的例子中,我只想允许我的API域,所以我在android/app/src/res/xml/networksecurity config.xml
文件中添加了以下代码
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">Your IP/Domain</domain>
</domain-config>
</network-security-config>
本地服务器
您的IP/域
我希望这是有帮助的。谢谢大家! 解决方案一是将以下代码添加到
AndroidManifest.xml
文件中
android:usesCleartextTraffic="true"
但在我的例子中,我只想允许我的API域,所以我在android/app/src/res/xml/networksecurity config.xml
文件中添加了以下代码
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">Your IP/Domain</domain>
</domain-config>
</network-security-config>
本地服务器
您的IP/域
我希望这是有帮助的。谢谢大家!