Android上的React-Native fetch()网络请求失败

问题描述:

背景: 我正在使用react-native和rails并在Kindle Fire HD平板电脑上开发。 当我使用fetch尝试连接到本地运行在我的macbook上的rails服务器时,出现以下错误。Android上的React-Native fetch()网络请求失败

TypeError: Network request failed 
    at XMLHttpRequest.xhr.onerror (index.android.bundle:12432) 
    at XMLHttpRequest.dispatchEvent (index.android.bundle:15169) 
    at XMLHttpRequest.setReadyState (index.android.bundle:14953) 
    at XMLHttpRequest.__didCompleteResponse (index.android.bundle:14809) 
    at index.android.bundle:14904 
    at RCTDeviceEventEmitter.emit (index.android.bundle:5866) 
    at MessageQueue.__callFunction (index.android.bundle:4838) 
    at index.android.bundle:4654 
    at MessageQueue.__guard (index.android.bundle:4809) 
    at MessageQueue.callFunctionReturnFlushedQueue (index.android.bundle:4653) 

这是取回请求。

fetch('https://192.168.X.X:8080/api/collections', { 
    method: 'GET', 
    body: null, 
}) 
.then((response) => response.json()) 
.then((responseJson) => { 
    console.log(responseJson) 
    return success(responseJson); 
}) 
.catch((err) => { 
    errors(err); 
}); 

四处搜索,大部分建议表示将localhost/127.0.0.1替换为服务器的IP地址。但这似乎没有帮助。

+0

您可以使用浏览器(例如Chrome)从平板电脑访问api终端吗? –

确保您的Mac端口已打开,但不是默认情况下。还要确保防火墙关闭或允许连接通过。

+0

是的,防火墙关闭了。 –

+0

不知道在火灾平板电脑我会尝试使用axios或以前我曾经有过奇怪的问题与内置取自己,所以我现在不再使用它。 – Thomas

+0

刚刚尝试使用相同的get请求axios,我仍然收到网络错误。如果我运行通过控制台发送请求在我的MacBook上一切正常,但通过我的平板电脑发送似乎给我的网络错误。 –