![](/img/trans.png)
[英]React native TypeError: Network request failed with fetch()
[英]“TypeError: Network request failed” (React Native) Android fetch fails to work with local backend
我正在尝试使用我的本地后端从我的反应本机应用程序中获取,并且它一直失败。 提取甚至没有到达我的后端,我制作了调试消息并在我的浏览器上进行了测试,以确保 rest api 正常工作。 它适用于 iOS。 我还可以访问其他网站,因此互联网可以在模拟器上运行。
更新:在进一步调试并尝试其他获取请求后,我发现了一个不同的错误,也许它们是相关的?
error Could not open fetch.umd.js in the editor.
info When running on Windows, file names are checked against a whitelist to protect against remote code execution attacks. File names may consist only of alphanumeric characters (all languages), periods, dashes, slashes, and underscores.
我已经在我的 AndroidManifest.xml 中添加了"android:usesCleartextTraffic="true"
。我还尝试将我的 react native 升级到版本 63.4 ,但问题仍然存在。
它打印到控制台“[GET]http://127.0.0.1:8090/api/v1/user/check/email/test@gmail.com”但随后应用程序获取
“TypeError:网络请求失败”
之后立马。
当我在浏览器中尝试使用 URL 时,它会正确打印{"transok":"0","errno":"001003","errmsg":"","timestamp":"1612157175224","data":{}}
我已经为此沮丧了好几天,如果有人能带领我朝着正确的方向前进,我将是难以想象的伟大。 如果需要更多信息,请告诉我,我将编辑此问题以提供详细信息
这是无法正常工作的 fetch 代码:
const APP_SERVER_HOST = 'http://127.0.0.1:8090';
const API_BASE = APP_SERVER_HOST + '/api';
const API_VERSION = '/v1';
const API_HOST = API_BASE + API_VERSION;
isEmailExist(callback) {
var url = API_HOST + '/user/check/email/'+this.state.email;
var options = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
};
console.log('[GET] ' + url);
//url is http://127.0.0.1:8090/api/v1/user/check/email/test@gmail.com
//url should post {"transok":"0","errno":"001003","errmsg":"","timestamp":"1612157175224","data":{}}
fetch(url, options).then((response) => response.json())
.then((responseData) => {
var retCode = responseData.errno;
console.log('[RTN] ' + responseData);
console.log('[RTN] ' + JSON.stringify(responseData));
if(retCode == Env.dic.ERROR_EMAIL_NOT_REG) {
console.log('[RTN] error email not reg');
return callback(false);
} else if(retCode == Env.dic.ERROR_EMAIL_EXIST){
console.log('[RTN] error email exist');
return callback(true);
}
}).done();
}
127.0.0.1
代表模拟器的 localhost 地址。 如果你想访问你机器的本地主机,你必须用10.0.2.2
替换它。
有关为什么会这样的更多信息,请参阅 Android文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.